Example usage for org.w3c.dom Element removeAttributeNode

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

Introduction

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

Prototype

public Attr removeAttributeNode(Attr oldAttr) throws DOMException;

Source Link

Document

Removes the specified attribute node.

Usage

From source file:Main.java

public static void dupAttributes(Document doc) {
    Element root = doc.getDocumentElement();
    Element personOne = (Element) root.getFirstChild();

    Attr deptAttr = personOne.getAttributeNode("dept");
    personOne.removeAttributeNode(deptAttr);
    String deptString = deptAttr.getValue();
    personOne.setAttribute("dept", deptString + "updated");

    String mailString = personOne.getAttribute("mail");
    personOne.setAttribute("mail", mailString + "updated");

    String titleString = personOne.getAttribute("title");
    //personOne.removeAttribute("title");
    personOne.setAttribute("title", titleString + "updated");
}

From source file:Main.java

public static void removeAttributes(Element element) {
    NamedNodeMap namedNodeMap = element.getAttributes();
    while (namedNodeMap.getLength() > 0) {
        element.removeAttributeNode((Attr) namedNodeMap.item(0));
    }//from ww  w.  j a va  2  s  . co m
}

From source file:com.cloudseal.spring.client.namespace.Utility.java

public static void removeNode(final Element rootElement, final String xPathLocation)
        throws XPathExpressionException {
    final XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(new CloudSealNamespaceContext());
    final Node node = (Node) xPath.evaluate(xPathLocation, rootElement, XPathConstants.NODE);
    final short nodeType = node.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE:
        final Node parent = node.getParentNode();
        parent.removeChild(node);/*w w w  .ja  v a 2s.  c o m*/
        break;

    case Node.ATTRIBUTE_NODE:
        final Attr attribute = (Attr) node;
        final Element element = attribute.getOwnerElement();
        element.removeAttributeNode(attribute);
        break;

    default:
        throw new IllegalArgumentException("Not supported node type: " + nodeType);
    }
}

From source file:Main.java

public static void stripDuplicateAttributes(Node node, Node parent) {

    // The output depends on the type of the node
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        Node child = doc.getFirstChild();
        while (child != null) {
            stripDuplicateAttributes(child, node);
            child = child.getNextSibling();
        }//from   w  ww. j a va 2s  .c  o  m
        break;
    }

    case Node.ELEMENT_NODE: {
        Element elt = (Element) node;
        NamedNodeMap attrs = elt.getAttributes();

        ArrayList nodesToRemove = new ArrayList();
        int nodesToRemoveNum = 0;

        for (int i = 0; i < attrs.getLength(); i++) {
            final Node a = attrs.item(i);

            for (int j = 0; j < attrs.getLength(); j++) {
                final Node b = attrs.item(j);

                //if there are two attributes with same name
                if (i != j && (a.getNodeName().equals(b.getNodeName()))) {
                    nodesToRemove.add(b);
                    nodesToRemoveNum++;
                }
            }
        }

        for (int i = 0; i < nodesToRemoveNum; i++) {
            Attr nodeToDelete = (Attr) nodesToRemove.get(i);
            Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode();
            nodeToDeleteParent.removeAttributeNode(nodeToDelete);
        }

        nodesToRemove.clear();

        Node child = elt.getFirstChild();
        while (child != null) {
            stripDuplicateAttributes(child, node);
            child = child.getNextSibling();
        }

        break;
    }

    default:
        //do nothing
        break;
    }
}

From source file:DOMEdit.java

public static void dupAttributes(Document doc) {
        Element root = doc.getDocumentElement();
        Element personOne = (Element) root.getFirstChild();

        Attr deptAttr = personOne.getAttributeNode("dept");
        personOne.removeAttributeNode(deptAttr);
        String deptString = deptAttr.getValue();
        personOne.setAttribute("dept", deptString + " updated");

        String mailString = personOne.getAttribute("mail");
        personOne.setAttribute("mail", mailString + " updated");

        String titleString = personOne.getAttribute("title");
        //personOne.removeAttribute("title");
        personOne.setAttribute("title", titleString + " updated");
    }//from   www  .  j  a v  a 2s. c  om

From source file:Main.java

public static void removeAttributes(Element elem) {
    if (elem.hasAttributes()) {
        NamedNodeMap attributeMap = elem.getAttributes();
        // since node maps are live, hold attributes in a separate collection
        int n = attributeMap.getLength();
        Attr[] attributes = new Attr[n];
        for (int i = 0; i < n; i++)
            attributes[i] = (Attr) attributeMap.item(i);
        // now remove each attribute from the element
        for (int i = 0; i < n; i++)
            elem.removeAttributeNode(attributes[i]);
    }/*from  www. j  av a 2  s. co m*/
}

From source file:ar.com.zauber.commons.web.transformation.sanitizing.impl.DeletingElementNodeSanitizer.java

/** @see AbstractElementNodeSanitizer#processInvalidElementAttributes(
 * Element, List) *//*w w  w  .  j  a v a 2  s .co m*/
@Override
public final void processInvalidElementAttributes(final Element element, final List<Attr> invalidAttributes) {

    Validate.notNull(element);
    Validate.notNull(invalidAttributes);

    for (Attr attribute : invalidAttributes) {
        element.removeAttributeNode(attribute);
    }
}

From source file:com.dinochiesa.edgecallouts.EditXmlNode.java

private void replace(NodeList nodes, Node newNode, short newNodeType) {
    Node currentNode = nodes.item(0);
    switch (newNodeType) {
    case Node.ATTRIBUTE_NODE:
        Element parent = ((Attr) currentNode).getOwnerElement();
        parent.removeAttributeNode((Attr) currentNode);
        parent.setAttributeNode((Attr) newNode);
        break;//from ww w.  jav  a2 s .co  m
    case Node.ELEMENT_NODE:
        currentNode.getParentNode().replaceChild(newNode, currentNode);
        break;
    case Node.TEXT_NODE:
        currentNode.setNodeValue(newNode.getNodeValue());
        break;
    }
}

From source file:cc.siara.csv_ml.ParsedObject.java

/**
 * Performas any pending activity against an element to finalize it. In this
 * case, it adds any pending attributes needing namespace mapping.
 * //from   w w w . j av a  2 s.  com
 * Also if the node has a namespace attached, it recreates the node with
 * specific URI.
 */
public void finalizeElement() {
    // Add all remaining attributes
    for (String col_name : pendingAttributes.keySet()) {
        String value = pendingAttributes.get(col_name);
        int cIdx = col_name.indexOf(':');
        String ns = col_name.substring(0, cIdx);
        String nsURI = nsMap.get(ns);
        if (nsURI == null)
            nsURI = generalNSURI;
        Attr attr = doc.createAttributeNS(nsURI, col_name.substring(cIdx + 1));
        attr.setPrefix(ns);
        attr.setValue(value);
        ((Element) cur_element).setAttributeNodeNS(attr);
    }
    // If the element had a namespace prefix, it has to be recreated.
    if (!currentElementNS.equals("") && !doc.getDocumentElement().equals(cur_element)) {
        Node parent = cur_element.getParentNode();
        Element cur_ele = (Element) parent.removeChild(cur_element);
        String node_name = cur_ele.getNodeName();
        String nsURI = nsMap.get(currentElementNS);
        if (nsURI == null)
            nsURI = generalNSURI;
        Element new_node = doc.createElementNS(nsURI, currentElementNS + ":" + node_name);
        parent.appendChild(new_node);
        // Add all attributes
        NamedNodeMap attrs = cur_ele.getAttributes();
        while (attrs.getLength() > 0) {
            Attr attr = (Attr) attrs.item(0);
            cur_ele.removeAttributeNode(attr);
            nsURI = attr.getNamespaceURI();
            new_node.setAttributeNodeNS(attr);
        }
        // Add all CData sections
        NodeList childNodes = cur_ele.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.CDATA_SECTION_NODE)
                new_node.appendChild(node);
        }
        cur_element = new_node;
    }
    pendingAttributes = new Hashtable<String, String>();
}

From source file:jef.tools.XMLUtils.java

/**
 * //  w ww .j  ava  2  s.  c  om
 * 
 * @param element
 *            ?
 */
public static void clearAttribute(Element element) {
    for (Node node : toArray(element.getAttributes())) {
        element.removeAttributeNode((Attr) node);
    }
}