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 an array of all the Element children of a DOM node.
 *
 * @param  parent  parent node/*from   w  w w . j av a 2s  .  com*/
 * @return  children array
 */
public static Element[] getChildren(Node parent) {
    NodeList nodeList = parent.getChildNodes();
    int nnode = nodeList.getLength();
    List elList = new ArrayList(nnode);
    for (int i = 0; i < nnode; i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            elList.add((Element) node);
        }
    }
    return (Element[]) elList.toArray(new Element[0]);
}

From source file:Main.java

/**
 * Get a child element with a specific name.
 *
 * @param name Name of the requested child element.
 * @param elem Parent element.//from   ww  w .  j  a v  a  2s  . c  o m
 *
 * @return The first child element with the given name, or null if none found.
 */
public static Element getChildElement(String name, Element elem) {

    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Element && n.getNodeName().equals(name))
            return (Element) n;
    }
    return null;
}

From source file:Main.java

public static Node[] findChildren(Node node, String name) {
    List<Node> list = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    int count = childNodes.getLength();
    for (int i = 0; i < count; i++) {
        Node child = childNodes.item(i);
        if (child instanceof Element && (name == null || child.getNodeName().equals(name))) {
            list.add(child);//  ww w .  jav  a2s  .  co m
        }
    }
    return list.toArray(new Node[list.size()]);
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node parent, String tagName) {
    List<Node> eleList = new ArrayList<Node>();
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName)) {
            eleList.add(node);//from   w  w  w .ja  va 2 s. c o m
        }

    }
    return eleList;
}

From source file:Main.java

public static Element getFirstChildElementByTagName(Element element, String name) {
    boolean found = false;
    Element result = null;/*from  w ww . j  av a2  s  . co  m*/
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength() && !found; i++) {
        if (nl.item(i) instanceof Element) {
            if (name.equals(nl.item(i).getNodeName())) {
                result = (Element) nl.item(i);
                found = true;
            }
        }

    }
    return result;
}

From source file:Main.java

/**
 * Clone all the nodes contains in the provided node list, assuming they are of type {@link Element}, and return them in a {@link List}.
 *
 * @param nodelist Node list/*from ww  w. ja  va2  s . co m*/
 * @return {@link List} of cloned nodes
 */
public static List<Element> toElementList(NodeList nodelist) {
    ArrayList<Element> list = new ArrayList<Element>(nodelist.getLength());
    for (int i = 0; i < nodelist.getLength(); i++) {
        list.add((Element) nodelist.item(i));
    }
    return list;
}

From source file:Main.java

/**
 * Count Elements in Document by Tag Name
 * @param tag/*from   www  . j a v a 2  s .c o m*/
 * @param document
 * @return number elements by Tag Name
 */
public static int countByTagName(String tag, Document document) {
    NodeList list = document.getElementsByTagName(tag);
    return list.getLength();
}

From source file:Main.java

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

From source file:Main.java

public static Element getFirstElement(Element element, String elementName) throws IOException {
    NodeList list = element.getElementsByTagName(elementName);
    if (list.getLength() >= 1) {
        Element subElement = (Element) list.item(0);
        return subElement;
    } else {//from  w w w  .ja  v  a2s  . co m
        return null;
    }
}

From source file:Main.java

public static List<Node> getChildNodes(Node dataNode) {
    List<Node> returnList = new ArrayList<Node>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node);/*from  ww w .  j  a va  2  s . co  m*/
        }
    }

    return returnList;
}