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 Node findChildNode(Node parent, String childName) {
    NodeList childNodes = parent.getChildNodes();
    int childCount = childNodes.getLength();
    Node child;/*from  w  w  w  .j av a  2 s.  c o  m*/

    for (int i = 0; i < childCount; i++) {
        child = childNodes.item(i);

        if (child.getNodeName().equals(childName)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Checks if a <code>Node</code> has any descendant <code>Element</code> nodes.
 * //from   www  .j  a  v a  2 s .  c  o  m
 * @param   n   <code>Node</code> to be examined
 * 
 * @return  boolean
 */
public static boolean containsElements(Node n) {
    NodeList children = n.getChildNodes();

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

        if (children.item(i) instanceof Element) {
            return true;
        }

    }

    return false;
}

From source file:Main.java

public static String getText(Node node) {
    String value = "";

    NodeList children = node.getChildNodes();
    for (int k = 0; k < children.getLength(); k++) {
        Node child = children.item(k);
        if (child.getNodeType() == Node.TEXT_NODE) {
            value = child.getNodeValue();
        }/*w ww .j  a va 2s. c om*/
    }

    return value;
}

From source file:Main.java

public static String getChildNodeText(Node root, String childName) {
    NodeList nodeList = root.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        if (childName.equals(nodeList.item(i).getNodeName()))
            return nodeList.item(i).getTextContent();
    }//from w  ww.  ja  v a2 s.c  om
    return "";
}

From source file:Main.java

static private boolean existNode(Node node, String nodeName, String textValue) {
    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        if (childs.item(i).getNodeName().equals(nodeName)
                && childs.item(i).getTextContent().equals(textValue)) {
            return true;
        }//from  w w  w  .j a va 2 s . c o  m
    }
    return false;
}

From source file:Main.java

/**
 * get the value of an Element in the Xml Document.
 * finds the first occurance of the parent element and then searches its children
 * for the first occurance of the element and retrieves its value.
 * @param root the root Element.//  ww w. j a v a2 s. co  m
 * @param parent the name of the parent element to search for.
 * @param elemName the name of the child element to search for.
 * @return String the element value or null if not found.
 */
public static String getSubElementValue(Element root, String parent, String elemName) {
    NodeList nl = root.getElementsByTagName(parent);
    String value = null;
    if (null == nl) {
        return (null);
    }
    Node node = nl.item(0);
    nl = node.getChildNodes();
    if (null == nl) {
        return (null);
    }

    boolean found = false;
    for (int i = 0; !found && i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (elemName.equals(n.getNodeName())) {
            value = n.getFirstChild().getNodeValue().trim();
            found = true;
            break;
        } // if
    } // for
    return (value);
}

From source file:Main.java

/**
 * Finds the first child of a node with the given name
 * @param parent the parent node/*  w ww.ja  v a  2 s.c  o m*/
 * @param name the name
 * @return the child node
 */
public static Node findFirstChild(Node parent, String name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static String getXMLText(Node n, boolean trim) {
    NodeList nl = n.getChildNodes();
    return (nl.getLength() == 0 ? null : getXMLText(nl, 0, trim));
}

From source file:Main.java

public static void removeTextNodes(Node node) {
    NodeList nl = node.getChildNodes();

    int i = 0;/*from  ww w  .  j a v a 2s . co m*/
    while (i < nl.getLength())
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            node.removeChild(nl.item(i));
        else
            i++;
}

From source file:Main.java

public static String getCDATAContent(Node node) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                CDATASection cdata = (CDATASection) child;
                return cdata.getData();
            }/*from w  w  w  . j  a v a  2  s  .c  o m*/
        }
    }
    return null;
}