Example usage for org.w3c.dom NodeList getLength

List of usage examples for org.w3c.dom NodeList getLength

Introduction

In this page you can find the example usage for org.w3c.dom NodeList getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

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);/*from  www  .  ja va 2s  .co m*/
        } else if (child instanceof Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}

From source file:Main.java

public static Element getFirstChildElementIgnoreCase(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);/*from  w ww .j  a v a 2 s  .  c  om*/
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (name == null || node.getNodeName().equalsIgnoreCase(name)))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

/**
 * Get Element by according tag/*from w w w  .ja v  a  2s .  c om*/
 * Only one such element should exist
 *
 * @param document document to search
 * @param tag tag to look for in document
 * @return corresponding element
 */
public static Element getSingleAppearingElementByTag(Document document, String tag) {
    NodeList elementList = document.getElementsByTagName(tag);
    if (elementList.getLength() != 1)
        return null;

    Node nNode = elementList.item(0);
    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
        return (Element) nNode;
    }
    return null;
}

From source file:Main.java

public static List<Node> getDescendents(Node node, String nodeName) throws IOException {
    List<Node> childList = new ArrayList<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(nodeName)) {
            childList.add(child);/*from   w  w w.  j  a  v a2s.c om*/
        }
    }
    return childList;
}

From source file:Main.java

public static Element readFirstChild(Node parentNode, String nodeName) {
    if (parentNode != null) {
        NodeList children = parentNode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeName().equals(nodeName))
                return (Element) children.item(i);
        }//from   w  w w.  j a v  a2s  . co  m
    }
    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   w  w  w .  j a  v  a  2s.co  m*/
    return buffer.toString();
}

From source file:Main.java

public static Element getFirstChild(Element tag, String childTagName) {
    NodeList nodes = tag.getElementsByTagName(childTagName);
    if (nodes == null || nodes.getLength() == 0) {
        return null;
    }/*from   w w w .  j a v  a2s  . c  o m*/

    return (Element) nodes.item(0);
}

From source file:Main.java

private static void findNodesNamed(Node node, String lookForName, Collection<Node> ret) {
    if (node.getNodeName().equals(lookForName)) {
        ret.add(node);//from w ww  .j  a v  a  2  s . c om
    } else {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); ++i) {
            findNodesNamed(list.item(i), lookForName, ret);
        }
    }
}

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  a  v  a 2s.co  m*/
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            return n;
        }
    }
    return null;
}

From source file:Main.java

public static Element find(Element element, String name) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(name)) {
            return (Element) child;
        }/* ww w.j a v  a2  s .c o m*/
    }
    return null;
}