Java XML Attribute Copy copyAttributes(final Element destElement, final Element srcElement)

Here you can find the source of copyAttributes(final Element destElement, final Element srcElement)

Description

copy Attributes

License

Apache License

Declaration

public static void copyAttributes(final Element destElement, final Element srcElement) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;

import java.util.Map;
import javax.xml.namespace.QName;

import org.w3c.dom.Attr;

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

public class Main {
    public static void copyAttributes(final Element destElement, final Element srcElement) {
        final NamedNodeMap attribs = srcElement.getAttributes();
        for (int i = 0; i < attribs.getLength(); i++) {
            final Attr attr = (Attr) attribs.item(i);
            final String uri = attr.getNamespaceURI();
            final String qname = attr.getName();
            final String value = attr.getNodeValue();
            if (uri == null && qname.startsWith("xmlns")) {
            } else {
                destElement.setAttributeNS(uri, qname, value);
            }/*from  www  .  j  a  v a2 s .co  m*/
        }
    }

    public static Map<QName, String> getAttributes(final Element el) {
        final Map<QName, String> attmap = new HashMap<QName, String>();
        final NamedNodeMap attribs = el.getAttributes();
        for (int i = 0; i < attribs.getLength(); i++) {
            final Attr attr = (Attr) attribs.item(i);
            final String name = attr.getName();
            final QName qname = resolveQName(el, name);
            final String value = attr.getNodeValue();
            attmap.put(qname, value);
        }
        return attmap;
    }

    public static QName resolveQName(final Element el, final String qualifiedName) {
        QName qname;
        String prefix = "";
        String namespaceURI = "";
        String localPart = qualifiedName;

        final int colIndex = qualifiedName.indexOf(":");
        if (colIndex > 0) {
            prefix = qualifiedName.substring(0, colIndex);
            localPart = qualifiedName.substring(colIndex + 1);

            if ("xmlns".equals(prefix)) {
                namespaceURI = "URI:XML_PREDEFINED_NAMESPACE";
            } else {
                Element nsElement = el;
                while (namespaceURI.equals("") && nsElement != null) {
                    namespaceURI = nsElement.getAttribute("xmlns:" + prefix);
                    if (namespaceURI.equals("")) {
                        nsElement = getParentElement(nsElement);
                    }
                }
            }
            if (namespaceURI.equals("")) {
                throw new IllegalArgumentException("Cannot find namespace uri for: " + qualifiedName);
            }
        }

        qname = new QName(namespaceURI, localPart, prefix);
        return qname;
    }

    public static Element getParentElement(final Node node) {
        final Node parent = node.getParentNode();
        return parent instanceof Element ? (Element) parent : null;
    }
}

Related

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