Example usage for org.w3c.dom Element removeAttribute

List of usage examples for org.w3c.dom Element removeAttribute

Introduction

In this page you can find the example usage for org.w3c.dom Element removeAttribute.

Prototype

public void removeAttribute(String name) throws DOMException;

Source Link

Document

Removes an attribute by name.

Usage

From source file:Main.java

private static void removeXmlBase(Element e) {
    e.removeAttributeNS("http://www.w3.org/XML/1998/namespace", "base"); // NOI18N
    e.removeAttribute("xml:base"); // NOI18N
}

From source file:Main.java

/**
 * Sets a named attribute of an Element, or removes the attribute if the value is null;
 * @param element The element to search/*from  ww w.  java  2s  .co m*/
 * @param attr The name of the attribute to set or remove
 * @param value The value to assign, or NULL to remove
 */
public synchronized static void setOrRemoveAttribute(Element element, String attr, String value) {
    if (value == null) {
        element.removeAttribute(attr);
    } else {
        element.setAttribute(attr, value);
    }
}

From source file:DOMEdit.java

public static void delAttribute(Document doc) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        person.removeAttribute("number");
        person.removeAttribute("dept");
    }/* w  w  w . j a v a  2  s .  c  om*/

From source file:Main.java

/**
 * Remove all attribute from the specified element
 *///from  ww w  . j ava 2  s .c om
public static void removeAllAttributes(Element element) {
    final NamedNodeMap nodeMap = element.getAttributes();

    for (int i = 0; i < nodeMap.getLength(); i++)
        element.removeAttribute(nodeMap.item(i).getNodeName());
}

From source file:Main.java

/**
 * Sets the Map as DOM attributes on the given Element.
 * <p>/*from w  ww  .j ava2 s .  c  o m*/
 * This implementation uses <code>element.setAttribute</code>. Therefore if the element already
 * has attributes, the new attributes are added amongst them. If attributes with the same name
 * already exist, they are overwritten.
 */

public static void setMapAsAttributes(Element element, Map<String, String> attributes) {

    if (attributes == null) {
        return;
    }

    for (Map.Entry<String, String> entry : attributes.entrySet()) {

        String value = entry.getValue();

        if (value == null) {
            element.removeAttribute(entry.getKey());
            continue;
        }

        element.setAttribute(entry.getKey(), value);
    }
}

From source file:Main.java

/**
 *
 * @param element// w  w w  .j  a  va2 s  .  c  o  m
 * @param attributeName
 * @param matches
 */
private static void removeAttributeLoopElement(Element element, String attributeName, String... matches) {

    if (element.hasAttribute(attributeName)) {
        if (matches(element.getTagName(), matches)) {
            element.removeAttribute(attributeName);
        }
    }

    NodeList nodeList = element.getChildNodes();
    int length = nodeList.getLength();
    for (int seq = 0; seq < length; seq++) {
        Node node = nodeList.item(seq);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (element.hasAttribute(attributeName)) {
                if (matches(element.getTagName(), matches)) {
                    removeAttributeLoopElement((Element) node, attributeName, matches);
                }
            }
        }
    }
}

From source file:com.evolveum.midpoint.util.UglyHacks.java

public static void unfortifyNamespaceDeclarationsSingleElement(Element element) {
    String fortifiedXmlnss = element.getAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME);
    if (element.hasAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME)) {
        element.removeAttribute(FORTIFIED_NAMESPACE_DECLARATIONS_ELEMENT_NAME);
    }//from   ww  w  .ja v a  2s.  c om
    if (StringUtils.isBlank(fortifiedXmlnss)) {
        return;
    }
    String[] xmlnss = StringUtils.split(fortifiedXmlnss, FORTIFIED_NAMESPACE_DECLARATIONS_SEPARATOR);
    for (String xmlns : xmlnss) {
        String[] prefixAndUrl = StringUtils.split(xmlns, "=", 2);
        String prefix = prefixAndUrl[0];
        String url = prefixAndUrl[1];
        if (!DOMUtil.hasNamespaceDeclarationForPrefix(element, prefix)) {
            DOMUtil.setNamespaceDeclaration(element, prefix, url);
        }
    }
}

From source file:XMLUtils.java

public static void replaceAttribute(Element element, String attr, String value) {
    if (element.hasAttribute(attr)) {
        element.removeAttribute(attr);
    }//from  www. ja v  a 2s  . co m
    element.setAttribute(attr, value);
}

From source file:Main.java

public static void removeInvalidAttributes(Element element, String... validAttributeNames) {
    Set<String> validNames = new HashSet<String>();
    for (String name : validAttributeNames) {
        validNames.add(name);/*from www.j  a va2  s .  c o m*/
    }
    boolean elementModified = false;
    NamedNodeMap nnm = element.getAttributes();
    for (int i = 0; i < nnm.getLength(); i++) {
        String attributeName = nnm.item(i).getNodeName();
        if (!validNames.contains(attributeName)) {
            element.removeAttribute(attributeName);
            elementModified = true;
        }
    }
    if (elementModified) {
        flagDocumentAsCorrected(element);
    }
}

From source file:Main.java

private static void fixupAttrsSingle(Element e) throws DOMException {
    removeXmlBase(e);//from ww  w .  j a  v a2  s  . c om
    Map<String, String> replace = new HashMap<String, String>();
    NamedNodeMap attrs = e.getAttributes();
    for (int j = 0; j < attrs.getLength(); j++) {
        Attr attr = (Attr) attrs.item(j);
        if (attr.getNamespaceURI() == null && !attr.getName().equals("xmlns")) { // NOI18N
            replace.put(attr.getName(), attr.getValue());
        }
    }
    for (Map.Entry<String, String> entry : replace.entrySet()) {
        e.removeAttribute(entry.getKey());
        e.setAttributeNS(null, entry.getKey(), entry.getValue());
    }
}