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 String getElementText(Element element) {
    NodeList nl = element.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        Node c = nl.item(i);/*from  w  ww.ja  va 2 s  .  c o  m*/
        if (c instanceof Text) {
            return ((Text) c).getData().trim();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Searches parent node for matching child nodes.
 *
 * @param node     The parent node//  w w w  .ja v  a2  s  .c  o  m
 * @param nodeName The child node's element name
 * @return A list of matching child Nodes
 */
public static Iterator getNodes(Node node, String nodeName) {
    ArrayList nodes = new ArrayList();
    NodeList nl = node.getChildNodes();
    int nll = nl.getLength();
    for (int i = 0; i < nll; i++) {
        Node n = nl.item(i);
        if (n.getNodeName().equals(nodeName)) {
            nodes.add(n);
        }
    }
    return nodes.iterator();
}

From source file:Main.java

/**
 * get single element by tag name/*from ww  w.  j  a va 2  s. c o m*/
 * @param element
 * @param tag
 * @return
 */
public static List<Element> getChildElements(Element element) {
    List<Element> elems = new ArrayList<Element>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            elems.add(e);
        }
    }
    return elems;
}

From source file:Main.java

/**
 * Search a child node by type// w  w w  .j  a v a 2s.c o  m
 * 
 * @param parent   the parent node
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByType(Node parent, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals("Node")) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);/*  w w  w .  j av  a 2s . c  om*/
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[0]);
}

From source file:Main.java

public static Element[] getElementsByName(Element parent, String name) {
    ArrayList resList = new ArrayList();
    NodeList nl = getNodeList(parent);
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);//from w w w . ja va  2 s.  c  o m
        if (nd.getNodeName().equals(name)) {
            resList.add(nd);
        }
    }
    Element[] res = new Element[resList.size()];
    for (int i = 0; i < resList.size(); i++) {
        res[i] = (Element) resList.get(i);
    }
    logger.debug(parent.getNodeName() + "'s children of " + name + "'s num:" + res.length);
    return res;
}

From source file:Utils.java

public static Element findElementOrContainer(Document document, Element parent, String element) {
    NodeList nl = parent.getElementsByTagName(element);
    if (nl.getLength() == 0) {
        return null;
    }/* w ww. j ava2s . co  m*/
    return (Element) nl.item(0);
}

From source file:Main.java

public static String getTagValue(Node node, String name) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(name);
        if (nodeList.getLength() > 0) {
            el = (Element) nodeList.item(0);
            nodeList = el.getChildNodes();

            if (nodeList.getLength() > 0) {
                return ((Node) nodeList.item(0)).getNodeValue();
            }/*from ww w .  j  a  v  a 2 s.c  o  m*/
        }

    }
    return "";
}

From source file:Main.java

static List<Node> children(Node parent, String childNS, String... childLocalName) {
    List<String> childNames = Arrays.asList(childLocalName);
    ArrayList<Node> result = new ArrayList<Node>();

    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (childNS.equals(child.getNamespaceURI()) && childNames.contains(child.getLocalName())) {

            result.add(child);/* ww w. j a  v  a2  s . c  om*/
        }
    }
    return result;
}

From source file:Main.java

/**
 * Get the text content of an element. If the element contains
 * mixed content (both elements and text), only the first text section
 * is returned./*from  w  ww .ja  va2s.  com*/
 *
 * @param element target element to retrieve text on, cannot be null.
 * @return text content of the element.
 */
public static String getElementText(Element element) {
    element.normalize();
    NodeList list = element.getChildNodes();
    int len = list.getLength();

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

        if (n.getNodeType() == Node.TEXT_NODE) {
            String s = n.getNodeValue();

            if (s != null) {
                return s.trim();
            }
        }
    }
    return null;
}