Example usage for org.w3c.dom Node getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

public static List<Node> getChildNodes(Node node, String childTag) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    if (node == null)
        return nodes;
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode != null && childNode.getLocalName() != null
                && childNode.getLocalName().equals(childTag)) {
            nodes.add(childNode);/* w w  w.  ja  va 2s  .  c o  m*/
        }
    }
    return nodes;
}

From source file:Main.java

public static HashMap<String, Node> getNodes(Node p_node) throws Exception {

    NodeList l_list = null;/*from   ww w. ja  v a 2  s  .  c  o m*/
    Node l_node = null;
    HashMap<String, Node> l_a = null;

    l_list = p_node.getChildNodes();
    if (l_list == null) {
        throw new Exception("XSD001: Unsupported / Invalid Element Type.");
    }
    l_a = new HashMap<String, Node>();
    for (int l_i = 0; l_i < l_list.getLength(); l_i++) {
        l_node = l_list.item(l_i);
        if (l_node.getNodeType() == Element.ELEMENT_NODE) {
            l_a.put(l_node.getNodeName(), l_node);
        }
    }

    return l_a;
}

From source file:Main.java

public static List<Element> getChildrenByTagName(Node parent, String tagName) {
    ArrayList<Element> elements = new ArrayList<Element>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            Element elem = (Element) children.item(i);
            if (tagName.equals(elem.getTagName())) {
                elements.add(elem);//from  www .  j a  va 2 s. co  m
            }
        }
    }
    return elements;
}

From source file:Main.java

public static String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE)
                    return data.getNodeValue();
            }//w w w .j  a va2s .  c  o m
        }
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildNodes(Node parent, String childName) {

    final List<Element> result = new ArrayList<Element>();

    final NodeList nodes = parent.getChildNodes();

    final int len = nodes.getLength();

    for (int i = 0; i < len; i++) {
        final Node n = nodes.item(i);
        if (!isElementNode(n)) {
            continue;
        }//ww w  .  ja v a2 s.  c  o  m
        if (n.getNodeName().equals(childName)) {
            result.add((Element) n);
        }
    }
    return result;
}

From source file:Main.java

public static String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if ((data.getNodeType() == Node.ATTRIBUTE_NODE)
                        && data.getNodeName().equalsIgnoreCase(attrName)) {
                    return data.getNodeValue();
                }/*from  w  ww. ja va  2s  .c om*/
            }
        }
    }
    return "";
}

From source file:Main.java

public static ArrayList<Element> getElementsByTagNameNS(Node element, String namespace, String name) {
    ArrayList<Element> childElements = new ArrayList<Element>();

    NodeList elements = element.getChildNodes();
    if (elements != null) {
        for (int i = 0; i < elements.getLength(); i++) {
            if (elements.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element currentElement = (Element) elements.item(i);
                if ((currentElement.getNamespaceURI() == null
                        || currentElement.getNamespaceURI().equals(namespace))
                        && currentElement.getNodeName().equals(name)) {

                    childElements.add(currentElement);
                }//from  w w w .  ja v  a2 s.  c  om
            }
        }
    }

    return childElements;
}

From source file:Main.java

public static Node getSingleChildNode(Node p_node) throws Exception {

    NodeList l_list = null;/*from   w w  w. ja v a2s.  c  o m*/
    Node l_node = null;
    ArrayList<Node> l_a = null;

    l_list = p_node.getChildNodes();
    if (l_list == null) {
        throw new Exception("XSD001: Unsupported / Invalid Element Type.");
    }
    l_a = new ArrayList<Node>();
    for (int l_i = 0; l_i < l_list.getLength(); l_i++) {
        l_node = l_list.item(l_i);
        if (l_node.getNodeType() == Element.ELEMENT_NODE) {
            l_a.add(l_node);
        }
    }
    if (l_a.size() == 1) {
        l_node = l_a.get(0);
    } else {
        throw new Exception("XSD003: Unsupported / Invalid Element Type Structure.");
    }

    return l_node;
}

From source file:Main.java

public static Node getFirstElementChild(Node parentNode) {
    Node element = null;/*w  ww .j a v a  2  s .  c  o m*/

    if (!isElement(parentNode)) {
        return null;
    } else {
        NodeList childList = parentNode.getChildNodes();
        for (int nodeIx = 0; nodeIx < childList.getLength(); nodeIx++) {
            Node node = childList.item(nodeIx);
            if (isElement(node)) {
                return node;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * used to parse out elements of a list such as this:
 * <node>//from   ww w .  j  ava  2s . c om
 *    <element>value1</element>
 *    <element>value2</element>
 *    <element>value3</element>
 * </node>
 *
 * for this example, called with Node pointing to <node> element and "element" 
 * in listElementName.
 * @param listNode pointer to list's "root" node
 * @param listElementName name of list element
 * @return Vector of list elements 
 */
public static Vector getListElements(Node listNode, String listElementName) {
    String tmp = "";
    Vector ret = null;

    NodeList children = listNode.getChildNodes();
    if (children != null) {
        ret = new Vector();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if (child.getNodeName().equalsIgnoreCase(listElementName)) {
                /* value is in element node's only (text) child */
                String value = child.getChildNodes().item(0).getNodeValue();
                if ((value != null) && !value.equals("")) {
                    ret.add(value);
                }
            }
        }
    }
    return ret;
}