Java XML Attribute Copy copyAttributes(Node from, Node to)

Here you can find the source of copyAttributes(Node from, Node to)

Description

copy Attributes

License

Open Source License

Declaration

public static void copyAttributes(Node from, Node to) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Attr;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    public static void copyAttributes(Node from, Node to) {
        NamedNodeMap map = from.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            Node attr = map.item(i);
            addAttribute(to, attr.getNodeName(), attr.getNodeValue());
        }//from   w ww .  j a  v a2  s.  c  om
    }

    public static Attr addAttribute(Node parent, String name, String value) {
        if (value == null)
            return null;
        Attr node = parent.getOwnerDocument().createAttribute(name);
        try {
            node.setValue(value);
            parent.getAttributes().setNamedItem(node);
        } catch (Exception e) {
            System.out.println("Problem rewriting " + parent.getNodeName() + "." + name + "='" + node.getValue()
                    + "' -> '" + value + "'");
        }
        return node;
    }
}

Related

  1. copyAttributes(Element elementFrom, Element elementTo)
  2. copyAttributes(Element from, Element to)
  3. copyAttributes(Element from, Element to, NodeFilter filter)
  4. copyAttributes(Element fromEl, Element toEl)
  5. copyAttributes(final Element destElement, final Element srcElement)
  6. copyAttributes(Node sourceNode, Element visualElement)
  7. copyAttributes(SOAPElement target, Element source)