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 printNodes(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        printNode(nodes.item(i));/*from w  ww  .j  av a  2  s .  c o  m*/
    }

}

From source file:Main.java

/**
 * Returns the first node that is a direct child of root with the coresponding
 * name.  Does not search children of children.
 *//*from  w  w  w  .jav  a  2  s  . co m*/
public static Element getFirstChild(Element root, String name) {
    NodeList nl = root.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (!(node instanceof Element))
            continue;
        Element ele = (Element) node;
        if (ele.getTagName().equals(name))
            return ele;
    }

    return null;
}

From source file:Main.java

public static String getConfigParam(Document xml, String xPath) throws Exception {
    //        NodeList nodeList = XPathAPI.selectNodeList(xml, xPath);
    NodeList nodeList = selectNodeList(xml, xPath);
    if (nodeList.getLength() == 0)
        return null;
    return nodeList.item(0).getNodeValue();
}

From source file:Main.java

public static String getTextContents(Element e) {
    if (!e.hasChildNodes())
        return null;

    StringBuffer buf = new StringBuffer();
    NodeList list = e.getChildNodes();
    int len = list.getLength();
    String text;//from  w  w  w  .  j  av  a2 s.c o m
    for (int i = 0; i < len; i++) {
        text = ((Node) list.item(i)).getNodeValue();
        if (text != null)
            buf.append(text);
    }
    return buf.toString();
}

From source file:Main.java

private static void print(Node e, String tab) {

    if (e.getNodeType() == Node.TEXT_NODE) {
        System.out.println(tab + e.getNodeValue());
        return;//from w w w .j a v  a2 s.  c  o  m
    }

    System.out.print(tab + e.getNodeName());

    NamedNodeMap as = e.getAttributes();
    if (as != null && as.getLength() > 0) {
        System.out.print(" attributes=[");
        for (int i = 0; i < as.getLength(); i++)
            System.out.print((i == 0 ? "" : ", ") + as.item(i));
        System.out.print("]");
    }
    System.out.println();

    if (e.getNodeValue() != null)
        System.out.println(tab + " " + e.getNodeValue());

    NodeList childs = e.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++)
        print(childs.item(i), tab + " ");
}

From source file:Main.java

public static Node findNodeByTagName(Document document, String tagName) {
    Node foundNode = null;//from w w  w.j av  a 2  s. co  m
    NodeList nodes = document.getElementsByTagName(tagName);
    if (nodes.getLength() > 0)
        foundNode = nodes.item(0);
    return foundNode;
}

From source file:Main.java

/**
 * Get the body value of a node/*from w ww.j  a v a  2  s  . c  o m*/
 */
public static String getNodeBodyValue(Node child) {
    String bodyValue = "";
    NodeList bodies = child.getChildNodes();
    for (int j = 0; j < bodies.getLength(); j++) {
        bodyValue += bodies.item(j).getNodeValue();
    }
    return bodyValue;
}

From source file:Main.java

public static int findFirstChildIndex(Element elem, String tagName) {
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        if (nl.item(i).getNodeName().equals(tagName))
            return i;
    }//from   w  w  w.j a v  a2 s  .  com
    return -1;
}

From source file:Main.java

public static Element getChildByName(Element parent, String nodeName) {
    NodeList nodes = parent.getChildNodes();
    int len = nodes.getLength();
    if (nodes != null && len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE
                    && nodes.item(i).getNodeName().equals(nodeName)) {
                return (Element) nodes.item(i);
            }//from   ww w. j av  a  2 s.com
        }
    }
    return null;
}

From source file:Main.java

public static Element getLastElement(Document dom, String tag) {
    NodeList nl = dom.getElementsByTagName(tag);

    int size = nl.getLength();
    if (size == 0) {
        return null;
    }/*from www. j  av  a 2s  . c  o m*/

    return (Element) nl.item(size - 1);
}