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 String getFirstElementText(Element parent, String tag) {
    NodeList nl = parent.getElementsByTagName(tag);
    if (nl.getLength() > 0) {
        return ((Element) nl.item(0)).getTextContent();
    }//  w ww  .ja  v a  2 s.  com
    return null;
}

From source file:Main.java

public static Node getFirstChildNodeNamed(Node node, String name) {
    NodeList nodes = node.getChildNodes();
    for (int x = 0; x < nodes.getLength(); x++) {
        if (nodes.item(x).getNodeName().equalsIgnoreCase(name) == true) {
            return nodes.item(x);
        }/*from  w  w  w.  j a v a2 s .c  o m*/
    }
    return null;
}

From source file:Main.java

protected static Node getNodeFromNodeList(NodeList list, String name) {

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeName().equals(name)) {
            return node;
        }//  w  ww .j ava 2 s. c om
    }
    return null;
}

From source file:Main.java

public static String getSingleValue(Element e, String s) {
    String value = null;// ww w .ja  va 2s.c  om
    NodeList nodeList = e.getElementsByTagName(s);
    if (nodeList.getLength() > 0) {
        Element _e = (Element) nodeList.item(0);
        NodeList eValue = _e.getChildNodes();
        if (eValue.getLength() > 0) {
            value = eValue.item(0).getNodeValue();
        }
    }

    return value;
}

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  a v  a 2 s .c o  m*/
    }
    return null;
}

From source file:Main.java

/**
 * @param node/* www.  j  a  v  a  2 s .  c o  m*/
 * @param name
 * @return the first child node with the given name or <code>null</code>
 * if none found
 */
public static Node findChild(Node node, String name) {
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            return child;
        }
    }
    return null;
}

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 getText(Element e) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//from  www.  j  av  a  2 s. c o m
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue();
        }
    }
    return "";
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);/* ww  w  . ja  va 2s.c  om*/
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);
        }
    }
}

From source file:Main.java

public static final Element getElementByTagName(Element parent, String name) throws IOException {
    final NodeList list = parent.getElementsByTagName(name);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    } else {/*from  w  w  w  . j  a  va 2  s .  c  o m*/
        throw new IOException(String.format("Expected one child element named \"%s\"", name));
    }
}