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 Iterable<Node> iterateOverChildren(final Node element) {
    return new Iterable<Node>() {

        NodeList childNodes = element.getChildNodes();
        int itemIndex = 0;
        int nItems = childNodes.getLength();

        @Override/*from  w w w. j  av  a2  s .c  om*/
        public Iterator<Node> iterator() {
            return new Iterator<Node>() {

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Node next() {
                    return childNodes.item(itemIndex++);
                }

                @Override
                public boolean hasNext() {
                    return itemIndex < nItems;
                }
            };
        }
    };
}

From source file:Main.java

public static List getChildNodesWithName(Node node, String name, String namespace) {
    List all = new ArrayList();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node child = node.getChildNodes().item(i);
        if (nodeHasNameNamespace(child, name, namespace)) {
            all.add(child);/*from  w  w  w  .  j a  v  a  2 s  .  c o m*/
        }
    }

    return all;
}

From source file:Main.java

public static List<Node> getGrandSonElementsByTagName(Element ele, String parentName, String eleName) {

    NodeList nl = ele.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }//from   www. ja  v  a  2 s.  co m
    Node item = nl.item(0);
    if (null == item) {
        return null;
    }
    NodeList subNodeList = item.getChildNodes();
    List<Node> childEles = new ArrayList<Node>();
    Node node = null;
    for (int i = 0; i < subNodeList.getLength(); i++) {
        node = subNodeList.item(i);

        if (node != null) {
            if (node instanceof Element && eleName.equals(node.getNodeName())
                    || eleName.equals(node.getLocalName())) {
                childEles.add(node);
            }
        }
    }

    return childEles;
}

From source file:Main.java

private static Object getElementValue(Node node) {
    NodeList nodes = node.getChildNodes();
    int childCount = nodes.getLength();
    int childElementCount = 0;
    for (int i = 0; i < childCount; i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            childElementCount++;//from   w  ww.  j  a  va  2s . c  o m
        }
    }
    if (childElementCount == 0) {
        return node.getTextContent();
    }
    Map<String, Object> map = new LinkedHashMap<>(childElementCount);
    for (int i = 0; i < childCount; i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        String childName = child.getNodeName();
        Object childValue = child.hasChildNodes() ? toObject(child) : null;
        // auto detect repeating elements
        if (map.containsKey(childName)) {
            Object temp = map.get(childName);
            if (temp instanceof List) {
                List list = (List) temp;
                list.add(childValue);
            } else {
                List list = new ArrayList(childCount);
                map.put(childName, list);
                list.add(temp);
                list.add(childValue);
            }
        } else {
            map.put(childName, childValue);
        }
    }
    return map;
}

From source file:Main.java

public static String getTextForNode(Node node) {
    StringBuffer sb = new StringBuffer();

    NodeList children = node.getChildNodes();
    if (children.getLength() == 0)
        return null;

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

        if (n instanceof Text)
            sb.append(n.getNodeValue());
        else if (n instanceof EntityReference) {
            String s = getTextForNode(n);
            if (s == null)
                return null;
            else//from  ww  w .  j a v  a  2s  .c  o m
                sb.append(s);
        } else
            return null;
    }

    return sb.toString();
}

From source file:Main.java

public static Node[] findNodesByTagName(Node parentNode, String tagName) {
    ArrayList<Node> nodes = new ArrayList<Node>();

    for (int i = 0; i < parentNode.getChildNodes().getLength(); i++) {
        Node node = parentNode.getChildNodes().item(i);

        if (node.getNodeName().equals(tagName))
            nodes.add(node);//  www .ja  v  a  2 s .  c o  m

        if (node.hasChildNodes()) {
            Node[] foundChildNodes = findNodesByTagName(node, tagName);
            for (int j = 0; j < foundChildNodes.length; j++)
                nodes.add(foundChildNodes[j]);

        }
    }

    Node[] returnNodes = new Node[nodes.size()];
    return nodes.toArray(returnNodes);
}

From source file:Main.java

public static String getNodeValue(final Node parentNode, final String strNodeName,
        final String strDefaultValue) {
    return getNodeValue(parentNode.getChildNodes(), strNodeName, strDefaultValue);
}

From source file:Main.java

/**
 * This method creates a collection of child nodes from a parent node
 * @param root the parent node//  w ww .  j  a  va2 s  .c om
 * @return Collection - childern nodes
 */
public static Collection getCollection(Node root) {
    Collection collection = new ArrayList();
    if (root != null) {
        NodeList children = root.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (!nodeName.equalsIgnoreCase("#comment") && !nodeName.equalsIgnoreCase("#text")) {
                if (child.getChildNodes().getLength() > 1) {
                    collection.add(child);
                } else {
                    Node textChild = child.getFirstChild();
                    if (textChild == null) {
                        // don't accept nulls
                    } else {
                        collection.add(child.getFirstChild().getNodeValue());
                    }
                }
            }
        }
    }
    return collection;
}

From source file:Main.java

/**
 * Scans a node and all of its children for nodes of a particular type.
 * /*  w w  w  .ja  v a  2s .c  om*/
 * @param parent
 *            The parent node
 * @param nodeName
 *            The node name to search for
 * @return a List of all the nodes found matching the nodeName under the parent
 */
public static List<Node> getNodes(final Node parent, final String nodeName) {
    final List<Node> nodes = new ArrayList<Node>();
    final NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        final Node child = children.item(i);

        if (child.getNodeName().equals(nodeName)) {
            nodes.add(child);
        } else {
            nodes.addAll(getNodes(child, nodeName));
        }
    }
    return nodes;
}

From source file:Main.java

/**
 * Retrieves the number of Nodes in the tree rooted at the given Node
 * //from w w  w  .j  av a  2 s.  co m
 * @param node the root Node
 * @return the number of Nodes
 **/
public final static int size(final Node node) {
    if (node == null) {
        return 0;
    }

    final NodeList children = node.getChildNodes();
    int size = 1;
    for (int i = 0, n = size(children); i < n; i++) {
        size += size(children.item(i));
    }

    return size;
}