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

private static void printNote(NodeList nodeList) {

    for (int count = 0; count < nodeList.getLength(); count++) {

        Node tempNode = nodeList.item(count);

        // make sure it's element node.
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {

            // get node name and value
            System.out.println("\nNode Name =" + tempNode.getNodeName() + " [OPEN]");
            System.out.println("Node Value =" + tempNode.getTextContent());

            if (tempNode.hasAttributes()) {

                // get attributes names and values
                NamedNodeMap nodeMap = tempNode.getAttributes();

                for (int i = 0; i < nodeMap.getLength(); i++) {

                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());

                }/*w  w  w.j  a  v a2 s .  co  m*/

            }

            if (tempNode.hasChildNodes()) {

                // loop again if has child nodes
                printNote(tempNode.getChildNodes());

            }

            System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");

        }

    }

}

From source file:Main.java

/**
 * Returns next sibling element node/*from w  ww  . j  av  a 2 s . c  om*/
 * @param element
 * @return
 */
protected static Element getNextSiblingElement(Element element) {
    try {
        Node tmpNode = null;
        tmpNode = element.getNextSibling();

        while (tmpNode.getNodeType() != Node.ELEMENT_NODE) {
            //fix for structure that has more than two parents null 
            if (tmpNode.getNextSibling() == null)
                tmpNode = tmpNode.getParentNode();
            tmpNode = tmpNode.getNextSibling();
        }
        return (Element) tmpNode;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static Element getFirstElementChild(final Node node) {

    Element elem = null;/*w ww. j a  v  a2s  .  co m*/
    for (Node childNode = node.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            elem = (Element) childNode;
            return elem;
        }
    }
    return elem;

}

From source file:Main.java

public static Element GetFirstElementChild(Element tag) {
    Node n = tag.getFirstChild();
    while (n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();// ww w.j a  v a  2  s.  c  o m
    }
    Element e = (Element) n;
    return e;
}

From source file:Main.java

/**
 * Method to get the depth of a XML Element.
 * @param element the XML Element./*from w  ww  .  j  av  a2s  .c  om*/
 * @return the int depth of the XML Element.
 */
public static int getDepth(Element element) {
    Node parent = element.getParentNode();
    int depth = 0;
    while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
        depth++;
        parent = parent.getParentNode();
    }
    return depth;
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    if (nodeList.getLength() == 1) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }//from  w w  w  . j  a  va  2 s  .  c o  m
    }
    return null;
}

From source file:Main.java

public static String GetFirstInstance(NodeList nList) {
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null)
                return eElement.getTextContent();
        } // end if nnode.getnodetype()
    } //end for int temp
    return new String("");
}

From source file:Main.java

public static List<Element> getChildElements(Element elt, String name) {
    List<Element> list = new ArrayList<Element>();

    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(child.getNodeName()))
                list.add((Element) child);
    }//  w w  w .  j a  v  a 2 s .c o  m
    return list;
}

From source file:Main.java

/**
 * Gets the first element of a Node//from   w  w w.j a v a 2  s.  c  o m
 * @param parent Node
 * @return first element of a Node
 */
public static Element getFirstElement(Node parent) {
    Node n = parent.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();
    }
    if (n == null) {
        return null;
    }
    return (Element) n;
}

From source file:Main.java

public static Element getChildByAttrName(Node node, String attrName, String attrValue) {

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) n;
            if (attrValue.equals(el.getAttribute(attrName))) {
                return el;
            }/*from w ww  .  java  2 s .com*/
        }
    }

    return null;
}