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 the content of a simple text tag If there are more than one text node or other nodetypes it will return null.
 *
 * @param node//from w  ww . j  a va 2 s .com
 *            the node
 * @return the simple text value from node
 */
public static String getSimpleTextValueFromNode(Node node) {
    if (node != null && node.getChildNodes().getLength() == 1
            && node.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getChildNodes().item(0).getTextContent();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get textual content of a node(for text nodes only).
 * //from www .j a  v a2 s. co  m
 * @param node a text node.
 * @return node's textual content.
 */
public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getTextContent();
    }
    return "";
}

From source file:Main.java

public static String getValueByNodeName(Node parentNode, String nodeName) {
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        if (nodeList.item(i).getNodeName().equalsIgnoreCase(nodeName)) {
            return nodeList.item(i).getTextContent();
        }//  w  w w. j a  v  a2  s.  c  om
    }
    return null;
}

From source file:Main.java

private static Node findNode(Node node, String fieldName) {
    NodeList Nodes = node.getChildNodes();
    for (int i = 0; i < Nodes.getLength(); i++) {
        Node fieldNode = Nodes.item(i);
        String name = fieldNode.getNodeName();
        if (name != null && name == fieldName) {
            return fieldNode;
        }//from   w  w  w.  j  a v a 2s.  c om
    }
    return null;
}

From source file:Main.java

/**
 * Fetch node using its name/*from  www  . j  a  v  a 2s.c  om*/
 * 
 * @param node
 * @param name
 * @return
 */
public static Node fetchByName(Node node, String name) {
    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node child = list.item(i);
        if (child.getNodeName().equals(name)) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String childElementName) {
    NodeList l = parentNode.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node node = l.item(i);// w  w w  . j  ava  2  s  . c om
        if (node.getNodeName().equals(childElementName))
            return node;
    }
    return null;
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node != null) {
        Text bb = (Text) node.getChildNodes().item(0);
        return bb.getNodeValue();
    }//from  w ww .j a va2s  .  c o m
    return "";
}

From source file:Main.java

public static Node getChildNodeByName(Node parentNode, String nodeName) {
    NodeList nodes = parentNode.getChildNodes();
    Node childNode = null;//from  w  ww  . j  a v a 2  s. c om

    for (int i = 0; i < nodes.getLength(); i++) {
        if (nodes.item(i).getNodeName().equals(nodeName)) {
            childNode = nodes.item(i);
            break;
        }
    }
    return childNode;
}

From source file:Main.java

/**
 * /*from   ww w . ja v a2s.  co  m*/
 * @return one large string with the contents of all TextNodes, or null if
 *         there are non text nodes or no text nodes as children.
 */
public static String getContentsOfTextOnlyNode(Node n) {
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        return null;
    }

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            sb.append(node.getNodeValue());
        } else {
            return null;
        }
    }

    return sb.toString();
}

From source file:Main.java

/**
 * this function will go recursively deep to find the node with the name
 * @param n//ww  w  . ja va  2s  .c  o  m
 * @param name
 * @return
 */
public static Node getNodeByName(Node n, String name) {
    NodeList l = n.getChildNodes();
    if (l == null)
        return null;

    for (int i = 0; i < l.getLength(); i++) {
        if (l.item(i).getNodeName().equals(name))
            return l.item(i);
        else {
            Node n2 = getNodeByName(l.item(i), name);
            if (n2 != null) {
                return n2;
            }
        }
    }
    return null;
}