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

static void dumpXpath(Node node, PrintStream printer) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node item = list.item(i);
        printer.println("Xpath: " + getXPath(item));
        printer.println("Text Content: " + item.getTextContent());
        if (item.hasChildNodes()) {
            dumpXpath(item, printer);/*from  w w  w  . j  av a 2 s. c o m*/
        }
    }
}

From source file:Main.java

public static void getElementsByTagName(Node parent, String tagName, List<Node> result) {
    if (parent == null) {
        return;//  w ww  . jav a 2  s  . c o m
    }

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            result.add(child);
        } else if (child.hasChildNodes()) {
            getElementsByTagName(child, tagName, result);
        }
    }
}

From source file:Main.java

/**
 * Get the content of the first Text child element of the passed element.
 *
 * @param aStartNode//from  w  ww  . j a v  a2  s . c o  m
 *        the element to scan for a TextNode child
 * @return <code>null</code> if the element contains no text node as child
 */
@Nullable
public static String getFirstChildText(@Nullable final Node aStartNode) {
    if (aStartNode != null) {
        final NodeList aNodeList = aStartNode.getChildNodes();
        final int nLen = aNodeList.getLength();
        for (int i = 0; i < nLen; ++i) {
            final Node aNode = aNodeList.item(i);
            if (aNode instanceof Text) {
                final Text aText = (Text) aNode;

                // ignore whitespace-only content
                if (!aText.isElementContentWhitespace())
                    return aText.getData();
            }
        }
    }
    return null;
}

From source file:Main.java

public static int getCountOfChildNodes(Node node, String nodeName) {
    int count = 0;

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                count++;//  w ww . j a  va  2s . c o  m
            }
        }

    }

    return count;
}

From source file:Main.java

/**
 * List all child nodes with name sName//  ww  w.  ja v  a 2s.c  o m
 * NOTE: we assume no same name nodes are nested.
 * @param node
 * @param sName
 * @return Element
 */
public static ArrayList<Element> listChildElementsByName(Node node, String sName) {
    ArrayList<Element> aNodes = new ArrayList<Element>();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (sName.equals(n.getNodeName())) {
                aNodes.add((Element) n);
            } else {
                ArrayList<Element> nextNodes = listChildElementsByName(n, sName);
                if (nextNodes != null)
                    aNodes.addAll(nextNodes);
            }
        }
        // Don't search anything but elements
    }
    return aNodes;
}

From source file:Main.java

public static int XMLDeep(Node parent) {
    Node child;/*ww w  . jav  a2s .  co m*/
    NodeList childs;
    int n;
    int maxdeep, deep;
    maxdeep = 0;
    childs = parent.getChildNodes();
    for (n = 0; n < childs.getLength(); n++) {
        child = (Node) childs.item(n);
        deep = XMLDeep(child);
        if (deep > maxdeep)
            maxdeep = deep;
    }
    maxdeep += 1;
    return maxdeep;
}

From source file:Main.java

/**
 * Gets specified child node from the specified node.<br>
 * If there are several nodes which have same name,
 * the node which was appeared at 1st will be returned.<br>
 * This methods will not throw any Exception.
 * /*from   w  w w . j  ava  2 s. c  o  m*/
 * @param node the specified node.
 * @param name a name of the child node.
 * @return the child node when it is found, null otherwise.
 */
public static Node getChildNodeGentle(Node node, String namespace, String localname) {
    int count = 0;
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node tmp = list.item(i);
        if (nodeIsElementOf(tmp, namespace, localname)) {
            return tmp;
        }
    }
    return null;
}

From source file:Main.java

public static ArrayList<Node> getNodesWithName(Node parent, String name, boolean all_of_them) {
    ArrayList<Node> valid = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++)
        if (children.item(i).getNodeName().equalsIgnoreCase(name)) {
            valid.add(children.item(i));
            if (!all_of_them)
                break;
        }//from w  w w. ja  v a2s  . c o m
    return valid;
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    if (node != null) {
        NodeList children = node.getChildNodes();
        for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
            Node child = children.item(innerLoop);
            if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName)) {
                Node grandChild = child.getFirstChild();
                if (grandChild.getNodeValue() != null)
                    return grandChild.getNodeValue();
            }/*from ww  w .java 2 s.  c o m*/
        } // end inner loop
    }
    return returnString;
}

From source file:Main.java

public static Element findElementByAttribute(Node node, String tagName, String attrName, String attrValue) {
    Element result = null;//from  w  w  w.j  av  a2 s  .  co  m
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null) {
        return result;
    }
    for (int i = 0; i < nodeList.getLength(); ++i) {
        Element element = checkIfElement(nodeList.item(i), tagName);
        if (element != null && element.getAttribute(attrName).equals(attrValue)) {
            result = element;
            break;
        }
    }
    return result;
}