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

/**
 * Get element Node from NodeList./*from  ww w .j a v  a 2 s.c  o  m*/
 *
 * @param nodeList
 *            NodeList.
 * @return Element Node
 */
public static Node getNodeFromNodeList(NodeList nodeList) {
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
                return nodeList.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets node index/*w  w w .ja v a 2 s . co  m*/
 * @param parentNode note in which the search
 * @param node seeking node
 * @return node index. -1 if node does not exists
 */
public static int getNodeIndex(Node parentNode, Node node) {
    NodeList list = parentNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).equals(node)) {
            return i;
        }
    }
    return -1;
}

From source file:Main.java

public static Node getNodeByTagName(Element node, String name) {
    NodeList nodeList = node.getElementsByTagName(name);
    if (nodeList != null && nodeList.getLength() > 0)
        return nodeList.item(0);
    else/*from  w  w  w.j  a v a  2  s .  c o  m*/
        return null;
}

From source file:Main.java

static public Element getElementByName(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        throw new IllegalArgumentException("Cannot find node " + nodeName);

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

From source file:Main.java

public static String getXMLText(Node n, boolean trim) {
    NodeList nl = n.getChildNodes();
    return (nl.getLength() == 0 ? null : getXMLText(nl, 0, trim));
}

From source file:Main.java

/**
 * /*from  w w  w  . j  a v  a2  s  .co m*/
 * @return one large string with the contents of all TextNodes, or null if
 *         there are non text nodes or no text nodes as children.
 */
public static String getContentsOfTextOnlyNode(Node n) {
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        return null;
    }

    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            sb.append(node.getNodeValue());
        } else {
            return null;
        }
    }

    return sb.toString();
}

From source file:Main.java

public static Node findNode(Node parentNode, String key) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeName().equals(key)) {
            return childNode;
        }/*from  w  ww  .  j  a v  a  2  s . c  om*/
    }
    return null;
}

From source file:Main.java

public static String get_inner_text(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            return child.getNodeValue();
        if (child instanceof Text)
            return ((Text) child).getData();
        /*if (child instanceof TextNode)
           return ((TextNode)child).getData();*/
    }//www .j a  va  2  s  . c o m
    return null;
}

From source file:Main.java

static public Element getElementByName(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        return null;

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

From source file:Main.java

private static String getText(Node node) {
    if (node == null)
        return null;
    NodeList lst = node.getChildNodes();
    int size = lst.getLength();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);//from w w w. j av a  2  s .  c  om
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            sb.append(t.getData());
        }
    }
    return sb.toString();
}