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 String getElementValue(Node node, String nodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName)))
            return getElementValue(child);
    }/*  w  w  w.  j ava2  s  .co m*/
    return null;
}

From source file:Main.java

@SuppressWarnings({ "unchecked" })
public static <X extends Node> List<X> getChildNodes(Node node, Class<X> type) {
    final NodeList nodelist = node.getChildNodes();
    ArrayList<X> list = new ArrayList<X>(nodelist.getLength());
    for (int i = 0; i < nodelist.getLength(); i++) {
        final Node child = nodelist.item(i);
        if (type.isInstance(child)) {
            list.add((X) child);//from   w w  w .ja  va 2 s  .  c  o m
        }
    }
    return list;
}

From source file:Main.java

public static Node selectNode(Node node, String tag) {
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);//from   w  ww.  j av  a2 s .  co  m
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            return n;
        }
    }
    return null;
}

From source file:Main.java

public static Node getChildNode(Node node, String pathname) {
    if (node != null && node.getChildNodes() != null && node.getChildNodes().getLength() > 0) {
        for (int a = 0; a < node.getChildNodes().getLength(); a++) {
            if (pathname.equals(node.getChildNodes().item(a).getNodeName())) {
                return node.getChildNodes().item(a);
            }/*from www . j  a v  a  2  s.co  m*/
        }
    }
    return null;
}

From source file:Main.java

/**
 * Return the value of the node nodeXML// w w w  .j av a2  s  . c o m
 * @param nodeXML Node object
 */
public static String getNodeValue(Node nodeXML) {
    return nodeXML.getChildNodes().item(0).getNodeValue();
}

From source file:Main.java

public static List<Node> getChildElements(Node node) {
    NodeList children = node.getChildNodes();
    int childCount = children.getLength();
    List<Node> nodes = new ArrayList<Node>(childCount);
    for (int i = 0; i < childCount; i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(child);/*from w  ww.  ja v  a 2  s. co m*/
        }
    }
    return nodes;
}

From source file:Main.java

public static Node findFirstChild(Node node, String name) {
    NodeList childNodes = node.getChildNodes();
    int count = childNodes.getLength();
    for (int i = 0; i < count; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element && (name == null || child.getNodeName().equals(name))) {
            return child;
        }/*from   www  .j a va 2s.c  om*/
    }
    return null;
}

From source file:Main.java

public static Node getChildElement(Node parent, String elementName) {
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        String nodeName = childNode.getLocalName();
        if (elementName.equals(nodeName)) {
            return childNode;
        }//from  w  w w  .java2  s.c  o  m
    }
    return null;
}

From source file:Main.java

/**
 * get the node following the node in parameter
 * //from   w  w w  .j av  a  2  s  .c  o m
 * @param n
 *            the n
 * @return the node
 */
public static Node next(Node n) {
    if (n == null) {
        return null;
    }
    Node currentNode = n;
    if (currentNode.getChildNodes().getLength() > 0) {
        currentNode = currentNode.getFirstChild();
    } else {
        if (currentNode.getNextSibling() != null) {
            currentNode = currentNode.getNextSibling();
        } else {
            Node oldCurrentNode = currentNode;
            currentNode = currentNode.getParentNode().getNextSibling();
            while (oldCurrentNode != null && currentNode == null) {
                oldCurrentNode = oldCurrentNode.getParentNode();
                if (oldCurrentNode != null) {
                    currentNode = oldCurrentNode.getNextSibling();
                }
            }
        }
    }
    return currentNode;
}

From source file:Main.java

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