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 Element getElementByTagName(Document document, String element) {
    NodeList nodeList = document.getElementsByTagName(element);
    if (nodeList.getLength() > 0) {
        return (Element) nodeList.item(0);
    } else {/*ww w . j a v  a 2  s .  com*/
        return null;
    }
}

From source file:Main.java

public static String getStringValueForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            String output = node.getNodeValue().trim();
            return output;
        }//from  ww w . java2s . co m
    }
    return "";
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }/*from w ww .j a v  a 2 s .c o  m*/
            }
        }
    }
    return "";
}

From source file:Main.java

public static Element getFirstElementByTagName(Element parentElement, String tagName) {
    NodeList list = parentElement.getElementsByTagName(tagName);
    if (list.getLength() == 0)
        return null;
    else/*from  w w w .  java  2s.  co  m*/
        return (Element) list.item(0);
}

From source file:Main.java

/** Return first child with given name within given element. */
public static Element getFirst(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0, l = nl.getLength(); i < l; i++) {
        Node curr = nl.item(i);/*ww w . j av  a  2s.  c o  m*/
        if (curr.getNodeType() == Element.ELEMENT_NODE && curr.getNodeName().equals(name))
            return (Element) curr;
    }

    return null;
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String childElementName) {
    NodeList l = parentNode.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node node = l.item(i);//  w w w .  jav  a2 s .co m
        if (node.getNodeName().equals(childElementName))
            return node;
    }
    return null;
}

From source file:Main.java

private static Object nodeValue(Node node) {
    NodeList childList = node.getChildNodes();
    if (childList.getLength() == 0) {
        return "";
    } else if (childList.getLength() == 1 && childList.item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getTextContent().trim();
    } else {//w  ww .  j a  v a  2s.c om
        List<Object> value = new ArrayList<Object>();
        for (int i = 0; i < childList.getLength(); i++) {
            value.add(nodeValue(childList.item(i)));
        }
        return value;
    }
}

From source file:Main.java

static private boolean existNode(Node node, String nodeName, String textValue) {
    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        if (childs.item(i).getNodeName().equals(nodeName)
                && childs.item(i).getTextContent().equals(textValue)) {
            return true;
        }/*from w ww  .  j av a 2s.  c  o  m*/
    }
    return false;
}

From source file:Main.java

/**
 * Scans the tag's children and returns the first text element found
 *///from   www .  j a v  a  2  s  . co  m
public static String getTagText(Element ele) {
    NodeList nl = ele.getChildNodes();
    int size = nl.getLength();

    Node node = null;
    int i = 0;
    for (; i < size; i++) {
        node = nl.item(i);
        if (node instanceof Text)
            break;
    }
    if (i == size || node == null)
        return null;

    return ((Text) node).getData();
}

From source file:Main.java

private static Element findElement(Element root, String tag) throws IOException {
    NodeList elements = root.getElementsByTagName(tag);

    if (elements.getLength() == 0) {
        throw new IOException("Tag " + tag + " was expected and not found.");
    } else if (elements.getLength() != 1) {
        throw new IOException("Tag " + tag + " cannot have multiple definitions.");
    }/*w  ww. j a  v  a  2s .c o  m*/

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