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

/** Return a list of the elements that are direct children of the given
 * node./*from   w  w  w .ja v a  2 s .  co  m*/
 */
public static List<Element> getChildElements(Node node) {
    NodeList childNodes = node.getChildNodes();
    List result = new ArrayList(childNodes.getLength());
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node oneChild = childNodes.item(i);
        if (oneChild instanceof Element)
            result.add(oneChild);
    }
    return result;
}

From source file:Main.java

public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i)
        if (children.item(i) instanceof Element)
            return (Element) children.item(i);
    return null;/*from w w  w . ja v a2  s. co m*/
}

From source file:Main.java

/**
 * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree.
 */// www .jav a  2  s. c o  m
public static void stripEmptyTextNodes(Node n) {
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node c = children.item(i);
        if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE
                && c.getTextContent().trim().length() == 0) {
            n.removeChild(c);
            i--;
            length--;
            children = n.getChildNodes();
        } else {
            stripEmptyTextNodes(c);
        }
    }
}

From source file:Main.java

public static ImmutableList<Node> childrenOf(Node node) {
    NodeList children = node.getChildNodes();
    ImmutableList.Builder<Node> result = ImmutableList.builder();
    for (int i = 0, size = children.getLength(); i < size; i++) {
        result.add(children.item(i));//from  w  w  w.  j a va2s .  c  om
    }
    return result.build();
}

From source file:Main.java

/**
 * Removes all of the child nodes from a parent node.
 *//*  w w w. java2  s  .  c  o m*/
public static void emptyNode(final Node parent) {
    final NodeList childNodes = parent.getChildNodes();
    for (int i = childNodes.getLength() - 1; i >= 0; i--) {
        final Node childNode = childNodes.item(i);
        childNode.getParentNode().removeChild(childNode);
    }
}

From source file:Main.java

private static Object nodeValue(Node node) {
    NodeList childList = node.getChildNodes();
    if (childList.getLength() == 0) {
        return "";
    } else if (childList.getLength() == 1 && childList.item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getTextContent().trim();
    } else {//from  w  w  w . ja v  a  2  s . c  o  m
        List<Object> value = new ArrayList<Object>();
        for (int i = 0; i < childList.getLength(); i++) {
            value.add(nodeValue(childList.item(i)));
        }
        return value;
    }
}

From source file:Main.java

/**
 * @param node//from   w  w w  . jav  a 2  s . co m
 * @return
 */
public static Element getLastElement(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null || children.getLength() == 0) {
        return null;
    }
    int len = children.getLength();
    for (int i = len - 1; i >= 0; i--) {
        Node n = children.item(i);
        if (n instanceof Element) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

/**
 * @param node//from  ww  w. j  ava  2  s  . c o m
 * @return
 */
public static Element getFirstElement(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null || children.getLength() == 0) {
        return null;
    }
    int len = children.getLength();
    for (int i = 0; i < len; i++) {
        Node n = children.item(i);
        if (n instanceof Element) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static Element getSingleElementByName(Node node, String name) {
    NodeList nodeList = node.getChildNodes();
    int count = nodeList.getLength();
    for (int i = 0; i < count; i++) {
        Node child = nodeList.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            return (Element) child;
        }//from   w  w  w  . j av a 2s .com
    }
    return null;
}

From source file:Main.java

public static Node getFirstChildByTagName(Node parent, String tagName) {
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName))
            return node;
    }/*from  w  w w .ja va 2s.  c o  m*/
    return null;
}