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

/** Returns all child <code>Node</code>s with the given name stored in a
 *  <code>Vector</code>.//from w w w .java2 s.c o  m
 *
 *  @param node The parent node to be searched for
 *  @param name The element name to search for
 *  @return <code>Vector</code> of found </code>Node</code>s
 */
public static Vector getSubnodesByName(Node node, String name) {
    Vector retVal = null;
    NodeList nl = node.getChildNodes();
    if (nl != null && nl.getLength() != 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeName().equals(name)) {
                if (retVal == null)
                    retVal = new Vector();
                retVal.addElement(nl.item(i));
            }
        }
    }
    return retVal;
}

From source file:Main.java

private final static boolean hasChilds(final Element element) {

    final NodeList list = element.getChildNodes();
    final int length = list.getLength();
    for (int i = 0; i < length; i++) {
        final Node node = list.item(i);
        if (node instanceof Element) {
            return true;
        }/*from   w  w  w . ja va2  s  .co m*/
    }

    return false;
}

From source file:Main.java

public static boolean isSubTagExist(Node node, String tagName) {
    if (node == null) {
        return false;
    }// w w w. j av a  2 s. c  o m
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n instanceof Element) {
            if (((Element) n).getTagName().equals(tagName)) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static List<Element> getRealChilds(Node node) {
    List<Element> interlayer = new ArrayList<Element>();
    NodeList l = node.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);/*from   www  .j  a  va 2 s. co  m*/
        if (n instanceof Element) {
            interlayer.add(((Element) n));
        }
    }
    return interlayer;
}

From source file:Main.java

public static Element getElement(Element parentNode, String nodeName) {
    if (parentNode == null) {
        return null;
    }/*from w w  w . j a  v  a2s. co m*/
    NodeList nl = parentNode.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            if (nl.item(i).getNodeName().equals(nodeName)) {
                return (Element) nl.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

public static Collection<Node> search_nodes_by_attribute(Node root, String attr_name, String attr_value) {
    Collection<Node> result = new LinkedList<Node>();
    if (root instanceof Element) {
        if (((Element) root).hasAttribute(attr_name)
                && ((Element) root).getAttribute(attr_name).equals(attr_value))
            result.add(root);//from   w  w  w . ja va 2s  .c om
    }
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        Collection<Node> ret = search_nodes_by_attribute(child, attr_name, attr_value);
        result.addAll(ret);
    }
    return result;
}

From source file:Main.java

public static String getTextContent(Node node) {

    if (node == null)
        throw new NullPointerException();

    String textContent;//  w  w  w .  j a v  a2s .c  o m

    textContent = node.getTextContent();
    if (textContent != null)
        return xmlDecode(textContent);

    NodeList childNodes = node.getChildNodes();
    if (childNodes.getLength() > 0 && childNodes.item(0) instanceof Text)
        textContent = node.getNodeValue();
    if (textContent != null)
        return xmlDecode(textContent);

    return "";
}

From source file:Main.java

public static Node findChildNode(Node parent, String childName) {
    NodeList childNodes = parent.getChildNodes();
    int childCount = childNodes.getLength();
    Node child;//w  ww  .  j a va 2  s  .c  om

    for (int i = 0; i < childCount; i++) {
        child = childNodes.item(i);

        if (child.getNodeName().equals(childName)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}.
 *
 * @param elem the parent Element//from   ww  w.  ja va  2  s. c om
 * @return the String value of the CDATA section, or {@code null} if none
 *         found
 */
public static String getCdata(Element elem) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            CharacterData cdataNode = (CharacterData) node;
            return cdataNode.getData();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Checks if the given {@link NodeList} contains a child element.
 *
 * @param list The {@link NodeList} to check.
 * @return Whether the {@link NodeList} contains children.
 *///from w  ww .j  a va  2  s.c  om
public static boolean hasChildElement(NodeList list) {

    Node n;

    for (int i = 0; i < list.getLength(); i++) {
        n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }

    return false;

}