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

/**
 * Get the body value of a node/* w  w  w .jav a2s  . com*/
 */
public static String getNodeBodyValue(Node child) {
    String bodyValue = "";
    NodeList bodies = child.getChildNodes();
    for (int j = 0; j < bodies.getLength(); j++) {
        bodyValue += bodies.item(j).getNodeValue();
    }
    return bodyValue;
}

From source file:Main.java

public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // get child node
        Node childNode = list.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                    + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

            String nodeValue = childNode.getNodeValue();
            nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
            if (!nodeValue.isEmpty()) {
                System.out.println(nodeValue);
                bw.write(nodeValue);// w  ww.j  av a  2  s  .  c  o m
                bw.newLine();
            }
        }
        visitRecursively(childNode, bw);
    }
}

From source file:Main.java

public static Node getChildNode(Node node, String childNodeName) {
    final NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node childNode = children.item(i);
        if (childNode.getNodeName().equals(childNodeName)) {
            return childNode;
        }/*from  w w  w.j av a 2s  .c  o  m*/
    }
    return null;
}

From source file:Main.java

public static String getText(Node node) {
    String text = "";
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            text += child.getNodeValue() + " ";
        }//from w w w .j  av a 2  s.co  m
    }
    return text;
}

From source file:Main.java

/**
 * Get the first child Element of the supplied node.
 *
 * @param node The DOM Node.// w w  w.  j  a  v  a2  s.  c  o m
 * @return The first child element
 */
public static Element getFirstChildElement(Node node) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();

    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child != null && child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static void removeAllChilderenWithoutHeader(Node node, int remainChildCount) {
    NodeList childNodes = node.getChildNodes();
    List<Node> removeNodeList = new ArrayList<Node>();
    for (int i = remainChildCount; i < childNodes.getLength(); i++) {
        removeNodeList.add(childNodes.item(i));
    }//from w  w  w .  java  2s .c  om

    for (Node childNode : removeNodeList) {
        node.removeChild(childNode);
    }

}

From source file:Main.java

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

    //for (int i = 0; i < nl.getLength(); i++) 
    while (nl.getLength() > 0)
        node.removeChild(nl.item(0));//w w  w  .  ja  va  2s.c om

}

From source file:Main.java

/**
 * Returns the String value of the content of the Node specified.
 *
 * @param pElement the node whose content to get.
 *
 * @return the value of the content of the Node.  An empty String if the Node
 *         does not have any content.//www . ja  v  a 2s .  co  m
 */
public static String getNodeTextValue(Node pElement) {
    NodeList children = pElement.getChildNodes();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Text) {
            Text n = (Text) children.item(i);
            sb.append(n.getNodeValue());
        }
    }
    return sb.toString();
}

From source file:Main.java

public static String getDirectTextContent(Node aNode) {
    String result = "";
    NodeList nodeList = aNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node aSubNode = nodeList.item(i);
        if (aSubNode.getNodeType() == Node.TEXT_NODE) {
            result += aSubNode.getNodeValue();
        }/*from w  w w. j a va 2s . c  o m*/
    }
    return result;
}

From source file:Main.java

public static Node getChildNode(Node node, String childName) {
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node curNode = nodes.item(i);
        if (curNode.getLocalName() != null && curNode.getLocalName().equals(childName)) {
            return curNode;
        }//from ww  w  .ja v  a2  s. c  o m
    }
    return null;
}