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

/**gets the sibling element of a node
 * @param start the node to get the sibling of
 * @return the sibling node*//*from   ww w.jav  a  2s .c  om*/
public static Node GetNextSiblingElement(Node start) {
    if (start == null)
        return start;
    Node node = start.getNextSibling();
    if (node == null)
        return node;
    else if (node.getNodeType() != Node.ELEMENT_NODE)
        return GetNextSiblingElement(node);
    else
        return node;
}

From source file:Main.java

public static final List<Element> getChildElements(Element parent) {
    final List<Element> childElements = new ArrayList<Element>();

    final NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        final Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }/*from   w  w  w  . j a  v a2s  .co m*/
    }

    return childElements;
}

From source file:Main.java

public static String getChildNodeValueOf(Node node, String tagName) {
    if (tagName.equals(node.getNodeName())) {
        return getValueOf(node);
    }/*from   w  w  w. j  av  a 2 s . co m*/
    for (Node temp = node.getFirstChild(); temp != null; temp = temp.getNextSibling()) {
        if (temp.getNodeType() == Node.ELEMENT_NODE && tagName.equals(temp.getNodeName())) {
            return getValueOf(temp);
        }
    }
    return null;
}

From source file:Main.java

public static Vector<Element> getChildElemsByName(String name, Node parent) {
    Vector<Element> v = new Vector<Element>();
    Element E = null;//w  ww .j a v a  2  s.co m
    for (Node childNode = parent.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (childNode.getNodeName() == name) {
                E = (Element) childNode;
                v.add(E);
            }
        }
    }
    return v;
}

From source file:Main.java

/** Finds and returns the last child element node. */
public static Element getLastChildElement(Node parent) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }//  w w w  .j a  v a  2s. c o  m
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Returns a node's first child node that is an element.
 * @param parent/*from w ww  . j  a  va  2 s  . c  o  m*/
 * @return first child element, or null
 */
public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getNamedChildElements(Element parent, String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }/*from  www . j  av  a 2s  .  c om*/
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }/* w  ww  .j ava 2 s.  c  o m*/

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public final static Element subElement(Element superEle, String subName) {
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }/*from   w  w w . j  av a 2 s .  c  om*/
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                return (Element) node;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> elements(Element root) {
    List<Element> L = new ArrayList<Element>();
    if (root == null)
        return L;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e2 = Element.class.cast(n1);
        L.add(e2);//www.j av a 2 s .  c  o  m
    }
    return L;
}