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

/**
 *  Returns all nodes at the bottom of path from node.
 * If element begins with '@', indicates an attribute, eg "@id"
 * The '#text' element indicates that the node has a single text child.
 * @param node    Node to apply path to//from w w  w .j  a v  a2  s.  c  o  m
 * @param path    Path to apply
 * @return        All Nodes at bottom of path. List may be empty, but not null.
 */
static public List<Node> extractNodes(Node node, String path) {
    if (node == null)
        return new ArrayList<Node>();
    List<Node> result = new ArrayList<Node>();
    NodeList list = node.getChildNodes();
    if (path.equals("#text"))
        result.add(node.getFirstChild());
    else if (path.charAt(0) == '@')
        result.add(node.getAttributes().getNamedItem(path.substring(1)));
    else
        for (int j = 0; j < list.getLength(); j++)
            if (list.item(j).getNodeType() == Node.ELEMENT_NODE && list.item(j).getNodeName().equals(path))
                result.add(list.item(j));
    return result;
}

From source file:Main.java

public static NodeList ChangeNode(NodeList listNode, ArrayList<String> arrStrCompare, String strNewValue) {
    ArrayList<String> arrTempList = arrStrCompare;
    Iterator<String> iterator = arrTempList.iterator();

    while (iterator.hasNext()) {
        String strCompare = (String) iterator.next();
        iterator.remove();//from   www .  jav a  2s  . c  o m

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

            if (strCompare.equals(node.getNodeName())) {
                if (iterator.hasNext()) {
                    node = ChangeNode(node.getChildNodes(), arrTempList, strNewValue).item(0);
                    break;
                }
                node.setTextContent(strNewValue);

                break;
            }
        }
    }

    return listNode;
}

From source file:Main.java

public static String getText(final Node node) {
    final StringBuilder result = new StringBuilder();
    if (!node.hasChildNodes())
        return "";

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.TEXT_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            // Recurse into the subtree for text
            // (and ignore comments)
            result.append(getText(subnode));
        }/*from w  w w  .j a  va2 s. c om*/
    }

    return result.toString();
}

From source file:Main.java

private static void printNode(NodeList nodeList, int level) {
    level++;/*  www  .  java 2 s .  c  o m*/
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(node.getNodeName() + "[" + level + "]");
                printNode(node.getChildNodes(), level);
                // how depth is it?
                if (level > depthOfXML)
                    depthOfXML = level;
            }
        }
    }
}

From source file:Main.java

/**
 * Get specified child nodes by tag name.
 * // ww w.  java 2s  .  c  o m
 * @param parent
 *          the parent node
 * @param tagName
 *          the tag name
 * @param searchDeeper
 *          search deeper?
 * @return the child node list
 * @throws Exception
 *           on error
 */
public static List<Node> getChildNode(Node parent, String tagName, boolean searchDeeper) throws Exception {
    List<Node> list = new ArrayList<Node>();
    if (null != parent) {
        NodeList childrenList = parent.getChildNodes();
        int childrenCnt = childrenList.getLength();
        for (int i = 0; i < childrenCnt; i++) {
            Node child = childrenList.item(i);
            if (child.getNodeName().equals(tagName)) {
                list.add(child);
            }
            if (searchDeeper) {
                list.addAll(getChildNode(child, tagName, searchDeeper));
            }
        }
    }
    return list;
}

From source file:Main.java

public static Node findNode(Node root, String tag) {
    if (root != null && root.getLocalName() != null && root.getLocalName().equals(tag)) {
        return root;
    }// w w w  .  j av a 2 s  . c o m
    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        Node firstNode = findNode(childNode, tag);
        if (firstNode != null) {
            return firstNode;
        }
    }
    return null;
}

From source file:Main.java

public static ArrayList<Node> getNodesWithKey(Node parent, String key, Set<String> values,
        boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    Node current;/*  ww w . j ava  2 s . c om*/
    for (int i = 0; i < children.getLength(); i++) {
        current = children.item(i);
        NamedNodeMap attrs = current.getAttributes();
        if (attrs != null) {
            Node keynode = attrs.getNamedItem(key);
            if (keynode != null)
                if (values == null || values.contains(keynode.getNodeValue())) {
                    valid.add(current);
                    if (!all_of_them)
                        break;
                }
        }
    }
    return valid;
}

From source file:Main.java

public static List<String> getValuesFromDocumentByTagAndAttribute(Node parentNode, String tagName,
        String attributeName, String attributeValue) {
    ArrayList<String> values = new ArrayList<String>();

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

        if (childNode != null) {
            if (childNode.hasAttributes()) {
                for (int j = 0; j < childNode.getAttributes().getLength(); j++) {
                    Node attribute = childNode.getAttributes().item(j);
                    if (attribute.getNodeName().equals(attributeName)
                            && attribute.getNodeValue().equals(attributeValue)) {
                        values.add(childNode.getTextContent().trim());
                    }//from   w w w  . ja va2 s  .  c o m
                }
            }

            if (childNode.hasChildNodes()) {
                values.addAll(getValuesFromDocumentByTagAndAttribute(childNode, tagName, attributeName,
                        attributeValue));
            }
        }
    }

    return values;
}

From source file:Main.java

/**
   Return a list of all the children of a given node with a
   specific name/*  ww w.  ja v a2 s .  c o  m*/
 */
static public Vector<Node> findAllChildren(Node node, String child_name) {
    if (node == null)
        return null;

    Vector<Node> found_children = new Vector<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            found_children.add(child);
    }
    return found_children;
}

From source file:Main.java

static List<Node> children(Node parent, String childNS, String... childLocalName) {
    List<String> childNames = Arrays.asList(childLocalName);
    ArrayList<Node> result = new ArrayList<Node>();

    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (childNS.equals(child.getNamespaceURI()) && childNames.contains(child.getLocalName())) {

            result.add(child);//from  w w w .  ja va 2 s  . c om
        }
    }
    return result;
}