Example usage for org.w3c.dom Node ELEMENT_NODE

List of usage examples for org.w3c.dom Node ELEMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node ELEMENT_NODE.

Prototype

short ELEMENT_NODE

To view the source code for org.w3c.dom Node ELEMENT_NODE.

Click Source Link

Document

The node is an Element.

Usage

From source file:Main.java

public static int getCountOfChildNodes(Node node, String nodeName) {
    int count = 0;

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                count++;/*from   ww  w .j a  va2  s .c  o m*/
            }
        }

    }

    return count;
}

From source file:Main.java

public static Element getNextSiblingElement(Node node) {
    Node n = node.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();/*from  w  w w. j  av a  2 s .  c  om*/
    }
    return (Element) n;
}

From source file:Main.java

public static void createAttribute(Document doc, String nodeName, Map<String, String> map) {
    Node node = doc.getElementsByTagName(nodeName).item(0);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            element.setAttribute(entry.getKey(), entry.getValue());
        }//  w w w .j ava 2  s.  com
        //TransformerFactory factory = TransformerFactory.newInstance();
        //Transformer former = factory.newTransformer();
        //former.transform(new DOMSource(doc), new StreamResult(new File("src/shuiguo.xml")));
    }
}

From source file:Main.java

public static Element getNextSiblingElement(Node node, final String siblingName) {
    do {//from w  w w . java 2 s.com
        node = node.getNextSibling();
        if (node == null)
            return null;
    } while (node.getNodeType() != Node.ELEMENT_NODE || !node.getNodeName().equals(siblingName));
    return (Element) node;
}

From source file:Main.java

public static Element getRootElement(Document doc) {
    Node node = doc.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();/*w  w  w.j av a 2  s  .com*/
    }
    return null;
}

From source file:Main.java

public static Element getNode(Element e, String name) {
    Node n = e.getElementsByTagName(name).item(0);
    if (n == null)
        return e;
    else if (n.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) n;
    } else/*  w  w  w.j  a  va 2  s  .co  m*/
        return e;
}

From source file:Main.java

private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getAttributes().getNamedItem("id") != null) {
            final String id = node.getAttributes().getNamedItem("id").getNodeValue();
            idMap.put(id, (Element) node);
        }//from  w w w  .java2  s . c  om
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        registerIDs(doc, children.item(i), idMap);
    }

}

From source file:Main.java

public static Node findNamedElementNode(Node doc, String name) {
    Node[] elnodes = getChildNodes(doc, Node.ELEMENT_NODE);
    for (int i = 0; i < elnodes.length; ++i) {
        if (elnodes[i].getNodeName().equals(name)) {
            return elnodes[i];
        }/*from  w  w w  .  j a  v a2 s  .  c  om*/
    }
    return null;
}

From source file:Main.java

public static Element first(Element root, String localName) {
    if (root == null)
        return null;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        return Element.class.cast(n1);
    }//from  w  w  w . ja  va  2s . c  o  m
    return null;
}

From source file:Main.java

private static Node getSingleNodeElementByTagName(String tagName) throws Exception {
    NodeList list = getNodeList(tagName);
    Node node = null;/*from   w w  w.j  a v a2 s  . c  o m*/
    if (list.getLength() > 0 && list.item(0).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(0);
    } else {
        throw new Exception("Xpath Query did not result in a Node element. Check your Xpath expression");
    }
    return node;
}