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:Util.java

public static String getTextValue(Node node) {
    StringBuffer textValue = new StringBuffer();
    int length = node.getChildNodes().getLength();
    for (int i = 0; i < length; i++) {
        Node c = node.getChildNodes().item(i);
        if (c.getNodeType() == Node.TEXT_NODE) {
            textValue.append(c.getNodeValue());
        }//from w  ww  .  j  av a2 s  .  co m
    }
    return textValue.toString().trim();
}

From source file:Main.java

/**
 * Extract included filenames in the XML document, assuming that filenames are
 * provided with the attribute "href".//from  www .  j ava2 s.c  o m
 * 
 * @param xmlDocument the XML document
 * @return the filenames to include
 */
private static Vector<String> extractIncludedFiles(Document xmlDocument) {

    Vector<String> includedFiles = new Vector<String>();

    NodeList top = xmlDocument.getChildNodes();
    for (int i = 0; i < top.getLength(); i++) {
        Node topNode = top.item(i);
        NodeList firstElements = topNode.getChildNodes();
        for (int j = 0; j < firstElements.getLength(); j++) {
            Node midNode = firstElements.item(j);
            for (int k = 0; k < midNode.getChildNodes().getLength(); k++) {
                Node node = midNode.getChildNodes().item(k);
                if (node.hasAttributes() && node.getAttributes().getNamedItem("href") != null) {
                    String fileName = node.getAttributes().getNamedItem("href").getNodeValue();
                    includedFiles.add(fileName);
                }
            }

        }
    }
    return includedFiles;
}

From source file:Main.java

/**
 * Returns a list of child Elements of specified name.
 *//* w  w  w.  j  av  a2s .  c o m*/
public static List getChildElements(Node node, String nodeName) {
    NodeList childs = node.getChildNodes();
    return filterNodeListElements(childs, nodeName);
}

From source file:Main.java

public static Node getChild(Node parent, String nodename) {
    for (Node child : toList(parent.getChildNodes())) {
        if (child.getNodeName().equalsIgnoreCase(nodename)) {
            return child;
        }/*from w ww  .  j  ava2  s .  com*/
    }
    return null;
}

From source file:Main.java

public static Element getRootElement(final Node docWithARootElement) {

    if (docWithARootElement != null && docWithARootElement.getChildNodes() != null
            && docWithARootElement.getChildNodes().getLength() > 0) {
        int numOfChildNodes = docWithARootElement.getChildNodes().getLength();

        if (numOfChildNodes == 1) {
            return (Element) docWithARootElement.getFirstChild();
        } else {//from   w w  w .  j a v a 2  s . c  om
            for (int i = 0; i < numOfChildNodes; i++) {
                Node nodeToInspect = docWithARootElement.getChildNodes().item(i);

                if (nodeToInspect != null && nodeToInspect.getNodeType() == Node.ELEMENT_NODE) {
                    return (Element) nodeToInspect;
                }
            }
        }
    }

    return null;

}

From source file:Main.java

/**
 * Returns the children element of an XML node
 *///w ww .j  av a 2 s .c o  m
public static Iterable<Element> children(Node n) {
    final List<Element> e = new ArrayList<Element>();
    final NodeList ns = n.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        if (ns.item(i) instanceof Element) {
            e.add((Element) ns.item(i));
        }
    }
    return e;
}

From source file:Main.java

public static Collection<Element> getChildElements(Node node) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode instanceof Element) {
            elements.add((Element) childNode);
        }//ww  w  . ja  v  a 2  s  . c om
    }
    return elements;
}

From source file:Main.java

public static Node getFirstChildElementNode(Node node) {
    if (node == null)
        return (null);

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType())
            return (child);
    }//from   w  ww  .j ava 2  s.  c  om

    return (null);
}

From source file:Main.java

public static String getTextContent(Node node) {
    StringBuffer buffer = new StringBuffer();
    NodeList childList = node.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        Node child = childList.item(i);
        if (child.getNodeType() != Node.TEXT_NODE)
            continue; // skip non-text nodes
        buffer.append(child.getNodeValue());
    }//from   ww w  .ja v a  2s .c o  m
    return buffer.toString();
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String childName) {
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeName().equalsIgnoreCase(childName))
            return (node);
    }//  w  w w  .  ja  va2 s.c  o  m
    return (null);
}