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 getChildElementByTagName(Element ele, String childEleName) {

    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/* w  ww .j  a v a 2s .  c om*/
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static String getNodeString(Element elem, String name, int inx) {
    NodeList nl = elem.getElementsByTagName(name);
    if (inx >= nl.getLength())
        return null;
    Element element = (Element) nl.item(inx);
    return getSimpleElementText(element);
}

From source file:Main.java

/**
 * get single element by tag name/*ww  w  .j  a  v  a  2 s.c  o  m*/
 * @param element
 * @param tag
 * @return
 */
public static Element getElementByTagName(Element element, String tag) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element && ((Element) node).getTagName().equals(tag)) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static Node getFirstValidNode(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (isValidNode(children.item(i)))
            return children.item(i);
    }/*from   ww  w. j  av a  2s  . c o m*/

    return null;
}

From source file:Main.java

public static Node getNodeChildByName(Node parentNode, String childNodeName) {
    Node childNode = null;//  w w  w.  j a v a 2s.  c om
    NodeList childNodeList = parentNode.getChildNodes();
    int len = childNodeList.getLength();
    for (int i = 0; i < len; i++) {
        Node tmpChildNode = childNodeList.item(i);
        if (tmpChildNode.getNodeName().equals(childNodeName)) {
            return tmpChildNode;
        }
    }
    childNode = null;
    return childNode;
}

From source file:Main.java

public static Node getChildElement(Node parent, String elementName) {
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        String nodeName = childNode.getLocalName();
        if (elementName.equals(nodeName)) {
            return childNode;
        }//from   w  w  w .  ja  va  2s. com
    }
    return null;
}

From source file:Main.java

public static boolean isEmpty(@Nullable final NodeList aNL) {
    return aNL == null ? true : aNL.getLength() == 0;
}

From source file:Main.java

/**
 *  Sets the text for a node/*from   w w  w.j  a v  a 2 s .co m*/
 */
public synchronized static void setText(Node n, String text) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");
    if (text == null)
        throw new IllegalArgumentException("Node text argument cannot be null");

    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            nl.item(i).setNodeValue(text);
            return;
        }
    }

    Node textNode = n.getOwnerDocument().createTextNode(text);
    n.appendChild(textNode);
}

From source file:Main.java

/**
 * Set the value of the first tag found with the given name in the given document<br/>. If the tag with the
 * requested name doesn't have a child as textnode with the tagValue is created.
 *
 * @param doc/*from www .j a  v  a2  s .  c om*/
 * @param tagName
 * @param tagValue
 */
public static void setTagValue(Document doc, String tagName, String tagValue) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        if (tagList.item(0).getFirstChild() != null) {
            tagList.item(0).getFirstChild().setNodeValue(tagValue);
        } else {
            tagList.item(0).appendChild(doc.createTextNode(tagValue));
        }
    }
}

From source file:Main.java

public static String getElementValue(Node node, String nodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName)))
            return getElementValue(child);
    }/*from  w w w  .  j av a  2s.  c  om*/
    return null;
}