Example usage for org.w3c.dom Node getNodeType

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

Introduction

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

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static List<Node> getChildrenByTagName(Node parent, String tagName) {
    List<Node> eleList = new ArrayList<Node>();
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName)) {
            eleList.add(node);/*ww  w  .  j a  v  a  2s. c  o m*/
        }

    }
    return eleList;
}

From source file:Main.java

public static List<Element> extractElementsFromNodeList(NodeList config, String tag, boolean required) {
    List<Element> res = new ArrayList<Element>();
    for (int i = 0; i < config.getLength(); i++) {
        Node n = config.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tag)) {
            res.add((Element) config.item(i));
        }/*w ww .  ja v  a2 s  .c o  m*/
    }
    if (required && res.size() == 0) {
        throw new RuntimeException("Tag " + tag + " is required in configuration file");
    }
    return res;
}

From source file:Main.java

/**
 * Given an node, returns the child text node of this element.
 * //  w  w  w  .j a v a 2s  . c  o m
 * @param node
 *            the node to get the text node from
 * @return the text node that is a child of this node, or <CODE>null</CODE>
 *         if there is no such child
 */
public static String containedText(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.TEXT_NODE)
            continue;
        return ((Text) c).getData();
    }
    return null;
}

From source file:Main.java

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

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 a v  a2s .com
    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 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 w w.j a  v  a  2s.c  o  m
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns the first Element node in a NodeList
 * @param list the list to search//from w  ww  .ja v  a2 s.c  o m
 * @return the element or null if none is found
 */
public static Element getFirstElement(NodeList list) {
    if (list.getLength() == 0) {
        return null;
    }

    Node node = list.item(0);
    while (node.getNodeType() != Node.ELEMENT_NODE) {
        if (node.getNextSibling() == null) {
            return null;
        }
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static String get_inner_text(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            return child.getNodeValue();
        if (child instanceof Text)
            return ((Text) child).getData();
        /*if (child instanceof TextNode)
           return ((TextNode)child).getData();*/
    }//  w w  w .  j ava  2  s.  co m
    return null;
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search/*from   w ww .j  a v a2 s.c  om*/
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementWithTagName(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                return n;
        }
    }

    return null;
}

From source file:Main.java

public static String getElementValue(Node node, String nodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName)))
            return getElementValue(child);
    }//w w  w. ja  v  a2 s.c o m
    return null;
}