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 public 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.getNodeValue();
        }// w  w w . j a v a  2 s. com
    }
    return "";
}

From source file:Main.java

public static Node getChildNodeByName(Node node, String childName) {
    NodeList children = node.getChildNodes();
    for (int j = 0; j < children.getLength(); j++) {
        Node child = children.item(j);
        if (child.getNodeName().equals(childName)) {
            return child;
        }/* w  ww  .  j  a v a  2  s.com*/
    }
    return null;
}

From source file:Main.java

public static Node getFirstChild(Node node, String childName) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child.getNodeName().equals(childName)) {
            return child;
        }/*w ww .j  a v  a 2s  .  c  o m*/
    }
    return null;
}

From source file:Main.java

public static void setCDATAContent(Node node, String value) {
    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;
                cdata.setData(value);//from  ww w  . ja  va  2s . c  o  m
            }
        }
    }
}

From source file:Main.java

public static Node getChildByName(Node parent, String childName) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = (Node) children.item(i);
        if (child.getNodeName().equals(childName)) {
            return child;
        }//from  w  w w  .  j a  v a 2  s  .c  o m
    }
    return null;
}

From source file:Main.java

public static Node findNode(Node parentNode, String key) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeName().equals(key)) {
            return childNode;
        }//from www. java2  s .com
    }
    return null;
}

From source file:Main.java

public static Node get_child(Node parent, String child_name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            return child;
    }/* w ww.  j av a2 s  .c om*/
    return null;
}

From source file:Main.java

/**
 * Gets the String value of the node. //w  w  w .j a v  a2 s. c  o m
 If the node does not contain text then an empty String is returned
 * @param node the node of interest
 * @return the value of that node
 */
public static String getTextForNode(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null) {
        return "";
    }

    for (int i = 0; i < children.getLength(); i++) {
        Node childNode = children.item(i);
        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            return childNode.getNodeValue();
        }
    }

    return "";
}

From source file:Main.java

public static List<Node> childrenAsList(Node node) {
    final NodeList children = node.getChildNodes();
    return new AbstractList<Node>() {

        @Override/*from w ww  .j av  a  2  s  .c  o  m*/
        public Node get(int index) {
            return children.item(index);
        }

        @Override
        public int size() {
            return children.getLength();
        }

    };
}

From source file:Main.java

/**
 * Gets the text content of the child nodes.
 * This is the same as Node.getTextContent(), but exists on all
 * JDKs.//from  w w w .j  a  va2  s . co m
 */
public static String getTextContent(Node node) {
    return getText(node.getChildNodes());
}