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 getFirstNode(Element elem, String name) {
    NodeList nl = elem.getElementsByTagName(name);
    if (nl.getLength() > 0)
        return (Element) nl.item(0);
    return null;/*  ww  w. ja  va  2 s .c  o  m*/
}

From source file:Utils.java

/**
 * Return the first named Element found.  Null if none.
 * @param element the containing Element
 * @param name the tag name/*  w  w  w .  j  av a 2 s  .c o  m*/
 * @return matching Element (null if none)
 */
public static Element getElement(Element element, String name) {
    NodeList nodeList = getElementList(element, name);
    return (nodeList.getLength() == 0) ? null : (Element) nodeList.item(0);
}

From source file:Main.java

public static Element getChildElement(Element parent, String childName, String childNamespace) {
    NodeList ns = parent.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        Node n = ns.item(i);/* w ww .j  a  va  2s  . c om*/
        if (n instanceof Element) {
            Element child = (Element) n;
            if (childName.equals(child.getLocalName()) && childNamespace.equals(child.getNamespaceURI())) {
                return child;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getChildElementByTagNameNS(Element parent, String tagName, String nsName) {
    NodeList nl = parent.getChildNodes();
    int size = nl.getLength();
    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);/* w ww  .  j  a va2  s.co m*/
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (tagName.equals(node.getLocalName())) {
                String ns = node.getNamespaceURI();
                if (ns != null && ns.equals(nsName)) {
                    return (Element) node;
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;/* w  w w . jav  a2 s  .c o  m*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }
    return textVal;
}

From source file:Main.java

/**
 *   Gets the text for a node by concatenating child TEXT elements.
 */// w w w  .j a va 2s  .c  o m
public synchronized static String getText(Node n) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");

    StringBuilder b = new StringBuilder();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            b.append(nl.item(i).getNodeValue());
        else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
            b.append(nl.item(i).getNodeValue());
    }

    return b.toString().trim();
}

From source file:Main.java

public static String getText(Node node) {
    String text = "";
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            text += child.getNodeValue() + " ";
        }/*from   ww  w.j av a  2  s. c  o m*/
    }
    return text;
}

From source file:Main.java

/**
 * Returns the content of the first CDATA section node found under the specified node.
 * /* w  w w.  ja  v  a  2  s . com*/
 * @param node
 *            the node.
 * @return the content of the first CDATA section node.
 */
public static String getCDataSectionContent(Node node) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).getNodeType() == Document.CDATA_SECTION_NODE) {
            return list.item(i).getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static String getDirectTextContent(Node aNode) {
    String result = "";
    NodeList nodeList = aNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node aSubNode = nodeList.item(i);
        if (aSubNode.getNodeType() == Node.TEXT_NODE) {
            result += aSubNode.getNodeValue();
        }/*ww  w. j  a  v a  2  s  . com*/
    }
    return result;
}

From source file:Main.java

public static String findNodeValue(Element firstElement, String name) {
    String nodeValue = null;/*from w  w w.  j  a v  a  2 s.  co m*/
    NodeList firstNameList = firstElement.getElementsByTagName(name);
    Element firstNameElement = (Element) firstNameList.item(0);

    NodeList textFNList = firstNameElement.getChildNodes();

    if (textFNList.getLength() > 0) {
        nodeValue = (textFNList.item(0)).getNodeValue();
    }
    return nodeValue;
}