Example usage for org.w3c.dom NamedNodeMap getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   ww  w .ja  va 2  s .  c  om*/
    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  w  w  .j  av  a 2s .c  om
    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());
    }//from ww w  .j  a v  a2  s . c  om
}

From source file:Main.java

private static void clearAttributes(Node node) {
    final NamedNodeMap attributes = node.getAttributes();
    while (attributes.getLength() > 0) {
        attributes.removeNamedItem(attributes.item(0).getNodeName());
    }//from  w  w  w .j a va 2  s  .  co  m
}

From source file:Main.java

License:asdf

public static void dom(Element element) {
    NamedNodeMap attrs = element.getAttributes();

    int numAttrs = attrs.getLength();
    for (int i = 0; i < numAttrs; i++) {
        Attr attr = (Attr) attrs.item(i);
        String attrName = attr.getNodeName();
        System.out.println(attrName);
        String attrValue = attr.getNodeValue();
        System.out.println(attrValue);
    }/*from   w  w  w  .  j a v a2s .  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  w ww .j a  v a  2s. c  o m
    }
}

From source file:Main.java

private static void print(Node e, String tab) {

    if (e.getNodeType() == Node.TEXT_NODE) {
        System.out.println(tab + e.getNodeValue());
        return;//from   www.ja  v a  2s.co m
    }

    System.out.print(tab + e.getNodeName());

    NamedNodeMap as = e.getAttributes();
    if (as != null && as.getLength() > 0) {
        System.out.print(" attributes=[");
        for (int i = 0; i < as.getLength(); i++)
            System.out.print((i == 0 ? "" : ", ") + as.item(i));
        System.out.print("]");
    }
    System.out.println();

    if (e.getNodeValue() != null)
        System.out.println(tab + " " + e.getNodeValue());

    NodeList childs = e.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++)
        print(childs.item(i), tab + " ");
}

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  w w w.  j  a v  a2  s  . c  o m
    }
    return false;
}

From source file:Main.java

public static void edit(Document doc) {
    Element element = doc.getDocumentElement();
    Element element2 = doc.createElement("newname");
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }/*w ww .ja  va2 s.com*/
    while (element.hasChildNodes()) {
        element2.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(element2, element);
}

From source file:Main.java

public static Properties getElementAttributes(Element ele) {
    Properties ht = new Properties();
    NamedNodeMap nnm = ele.getAttributes();
    int len = nnm.getLength();
    Node tmpn = null;//from   w w w.j  a  v a 2  s.  c  o m
    for (int k = 0; k < len; k++) {
        tmpn = nnm.item(k);
        String tmps = tmpn.getNodeValue();
        if (tmps == null) {
            tmps = "";
        }
        ht.put(tmpn.getNodeName(), tmps);
    }
    return ht;
}