Example usage for org.w3c.dom NamedNodeMap item

List of usage examples for org.w3c.dom NamedNodeMap item

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the map.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w w  w .  java 2 s  .co m*/
    DocumentBuilder db = dbf.newDocumentBuilder();

    File file1 = new File("input1.xml");
    Document doc1 = db.parse(file1);
    Element rootElement1 = doc1.getDocumentElement();

    File file2 = new File("input2.xml");
    Document doc2 = db.parse(file2);
    Element rootElement2 = doc2.getDocumentElement();

    // Copy Attributes
    NamedNodeMap namedNodeMap2 = rootElement2.getAttributes();
    for (int x = 0; x < namedNodeMap2.getLength(); x++) {
        Attr importedNode = (Attr) doc1.importNode(namedNodeMap2.item(x), true);
        rootElement1.setAttributeNodeNS(importedNode);
    }

    // Copy Child Nodes
    NodeList childNodes2 = rootElement2.getChildNodes();
    for (int x = 0; x < childNodes2.getLength(); x++) {
        Node importedNode = doc1.importNode(childNodes2.item(x), true);
        rootElement1.appendChild(importedNode);
    }

    // Output Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(doc1);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from w  ww  . j a  va 2 s.  c o m*/
    DocumentBuilder db = dbf.newDocumentBuilder();

    File file1 = new File("input1.xml");
    Document doc1 = db.parse(file1);
    Element rootElement1 = doc1.getDocumentElement();

    File file2 = new File("input2.xml");
    Document doc2 = db.parse(file2);
    Element rootElement2 = doc2.getDocumentElement();

    // Copy Child Nodes
    NodeList childNodes2 = rootElement2.getChildNodes();
    for (int x = 0; x < childNodes2.getLength(); x++) {
        Node importedNode = doc1.importNode(childNodes2.item(x), true);
        if (importedNode.getNodeType() == Node.ELEMENT_NODE) {
            Element importedElement = (Element) importedNode;
            // Copy Attributes
            NamedNodeMap namedNodeMap2 = rootElement2.getAttributes();
            for (int y = 0; y < namedNodeMap2.getLength(); y++) {
                Attr importedAttr = (Attr) doc1.importNode(namedNodeMap2.item(y), true);
                importedElement.setAttributeNodeNS(importedAttr);
            }
        }
        rootElement1.appendChild(importedNode);
    }

    // Output Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(doc1);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);
}

From source file:Main.java

License:asdf

public static void dom(Element element) {
    NamedNodeMap attrs = element.getAttributes();
    while (attrs.getLength() > 0) {
        attrs.removeNamedItem(attrs.item(0).getNodeName());
    }//w  ww .java2  s .  co m
}

From source file:Main.java

public static void printAttributes(Element element) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        System.out.println("## prefix=" + node.getPrefix() + " localname:" + node.getLocalName() + " value="
                + node.getNodeValue());/*from  www.  j  a v  a 2  s  .c o m*/
    }
}

From source file:Main.java

public static void copyAttr(Element from, Element to) {
    NamedNodeMap nnm = from.getAttributes();
    for (int i = 0; i < nnm.getLength(); ++i) {
        String name = nnm.item(i).getNodeName();
        if (!to.hasAttribute(name)) {
            String value = nnm.item(i).getNodeValue();
            to.setAttribute(name, value);
        }//from  ww w .  j  ava  2s. c  om
    }
}

From source file:Main.java

/**
 * get attribute map from the element/*  w ww. j av a 2  s  .  com*/
 * @param el - element
 * @return mapping of attributes
 */
public static Map<String, String> getAttributes(Element el) {
    Map<String, String> list = new LinkedHashMap<String, String>();
    NamedNodeMap map = el.getAttributes();
    for (int i = 0; i < map.getLength(); i++) {
        list.put(map.item(i).getNodeName(), map.item(i).getNodeValue());
    }
    return list;
}

From source file:Main.java

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());
    }/* w  w  w  .  ja v  a 2s .c om*/
}

From source file:Main.java

public static boolean hasAttribute(Element element, String value) {
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node node = attributes.item(i);
        if (value.equals(node.getNodeValue())) {
            return true;
        }/*from  ww  w.j av a 2 s.c  o  m*/
    }
    return false;
}

From source file:Main.java

public static String getAttribute(Node node, String name) {
    NamedNodeMap attr = node.getAttributes();
    for (int n = 0; n < attr.getLength(); n++) {
        Node attribute = attr.item(n);
        if (attribute.getNodeName().equals(name))
            return attribute.getNodeValue();
    }/*from w ww.  j  a  v  a2s  .  c  o m*/
    return "";
}

From source file:Main.java

public static String getPrefix(org.w3c.dom.Element el, String ns) {
    NamedNodeMap atts = el.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (ns.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            return node.getPrefix();
        }/*w w  w  .j  av  a 2 s  .  co m*/
    }
    return null;
}