Example usage for org.w3c.dom Element getChildNodes

List of usage examples for org.w3c.dom Element getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element 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 boolean hasChild(Element parent, String nodeName) {
    NodeList childNodes = parent.getChildNodes();
    int length = childNodes.getLength();
    for (int i = 0; i < length; i++)
        if (childNodes.item(i).getNodeName().equals(nodeName))
            return true;
    return false;
}

From source file:Main.java

public static String getElementText(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return ((Text) child).getNodeValue();
        }/*w w w  .  ja  v a2 s  . c om*/
    }
    return "";
}

From source file:Main.java

public static Element getFirstChildElement(Element element) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            return (Element) node;
        }/*from  www . ja va  2  s.  c  o  m*/
    }
    return null;
}

From source file:Main.java

public static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);/* w w w  .  j av  a  2  s .  c  o  m*/
        } else if (child instanceof Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}

From source file:Main.java

public static String getBody(Element element) {
    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() == 0) {
        return null;
    }/*ww w  . j a va  2  s. co  m*/

    Node firstNode = nodes.item(0);
    if (firstNode == null) {
        return null;
    }

    return firstNode.getNodeValue();
}

From source file:Main.java

public static List<Node> getChildElementsByTagName(Element ele, String childEleName) {
    NodeList nl = ele.getChildNodes();
    List<Node> childEles = new ArrayList<Node>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/*from   w  w w . j a v a 2s.co  m*/
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            childEles.add(node);
        }
    }
    return childEles;
}

From source file:Main.java

public static List<Element> getChildElements(Element parentElement) {
    NodeList nodeList = parentElement.getChildNodes();
    List<Element> childElements = new ArrayList<Element>(nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(0);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) childNode);
        }/*w ww. j a  v a2 s.co  m*/
    }
    return childElements;
}

From source file:Main.java

public static Element getFirstChild(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);/*from   ww w . j  av  a  2  s  . c om*/
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static Element[] getChildrenByName(Element e, String name) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    LinkedList<Node> list = new LinkedList<Node>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);/*from   w ww .j  av a  2s  .c o  m*/
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            list.add(n);
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

/**
 * @see //http://www.java.net/node/667186
 *///from   w w w .  ja v  a 2 s.c  om
public static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);
        } else if (child instanceof Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}