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 Node search_node_by_attribute(Node root, String attr_name, String attr_value) {
    if (root instanceof Element) {
        if (((Element) root).hasAttribute(attr_name)
                && ((Element) root).getAttribute(attr_name).equals(attr_value))
            return root;
    }//from   w  w w .j  a v  a 2  s .c o m
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        Node ret = search_node_by_attribute(child, attr_name, attr_value);
        if (ret != null)
            return ret;
    }
    return null;
}

From source file:Main.java

public static Element getLastChild(Element parent, String name) {
    Element child = null;/*from  ww w.j a  va2 s. co  m*/
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            child = (Element) n;
        }
    }
    return child;
}

From source file:Main.java

static boolean hasChildNode(Element root, String childNodeName) {
    NodeList nl = root.getElementsByTagName(childNodeName);
    return (nl.getLength() > 0);
}

From source file:Main.java

public static List getChildrenElement(Node n, String childTagName) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);// w ww. j ava  2  s .  c  o  m

        if ((cn.getNodeType() == Node.ELEMENT_NODE) && (cn.getNodeName().equals(childTagName))) {
            lst.add(cn);
        }
    }
    return lst;
}

From source file:Main.java

public static List getChildrenElement(Node n) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);//from w ww  .ja  v a  2s.  c o m
        if (cn.getNodeType() == Node.ELEMENT_NODE) {
            lst.add(cn);
        }
    }

    return lst;
}

From source file:Main.java

public static Node getFirstChildElementNode(Node node) {
    if (node == null)
        return (null);

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType())
            return (child);
    }//from   www  .j  a v  a 2 s .  c  o m

    return (null);
}

From source file:Utils.java

/**
 * //from ww w  .  ja  v  a  2s.  co m
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * Get the first instance of an element by name.
 *
 * @param parent The parent to get the element from.
 * @param elementName The name of the element to look for.
 * @return The element or null if it is not found.
 *//*  ww w  .jav  a 2  s.  c o m*/
public static Element getElement(Element parent, String elementName) {
    Element retval = null;
    NodeList children = parent.getElementsByTagName(elementName);
    if (children.getLength() > 0) {
        retval = (Element) children.item(0);
    }
    return retval;
}

From source file:Main.java

/**
 * Gets the first child Element with a given name
 * //  w  w  w  . j  a  v a  2s  .co m
 * @param n
 *            the node get the children from
 * @param elementName
 *            the name of the child elements
 * @return the first child Element with a given name
 */
public static Element getElementByTagName(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    if (sz > 0)
        return (Element) subNodes.item(0);
    return null;
}

From source file:Main.java

public static Node getNode(Node node, String nodeName) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(nodeName);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0);
        }// w  w w.j a v  a  2  s  .c  o  m

    }

    return null;
}