Example usage for org.w3c.dom NodeList item

List of usage examples for org.w3c.dom NodeList item

Introduction

In this page you can find the example usage for org.w3c.dom NodeList item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the collection.

Usage

From source file:Main.java

public static Node[] getChildrenNamed(Node node, String name) {
    ArrayList v = new ArrayList();

    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node n = nList.item(i);
        if (n.getNodeName().equalsIgnoreCase(name)) {
            v.add(n);// w w w  .j  a  v a 2 s. co  m
        }
    }

    Node[] nodeArray = new Node[v.size()];
    nodeArray = (Node[]) v.toArray(nodeArray);
    return nodeArray;
}

From source file:Main.java

public static Collection getChildElements(Node node) {
    List elements = new ArrayList();
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode instanceof Element) {
            elements.add(childNode);/*from  w  w  w  .  j ava2 s . c o  m*/
        }
    }
    return elements;
}

From source file:Main.java

public static Node getUniqueNode(Node node, String xPathAsString) throws XPathExpressionException {
    NodeList nodeList = getNodeList(node, xPathAsString);
    return nodeList == null || nodeList.getLength() == 0 ? null : nodeList.item(0);
}

From source file:Main.java

public static List<Element> getChildren(Element element, String tagName) {
    List<Element> children = new ArrayList<>();
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (tagName != null && child.getTagName().equals(tagName))
                children.add(child);/*from w w w  .ja  v  a2s . c om*/
        }
    }
    return children;
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;/*from   w  w w.j a  v  a2  s  . c  om*/
    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

private static Node findNode(Node node, String fieldName) {
    NodeList Nodes = node.getChildNodes();
    for (int i = 0; i < Nodes.getLength(); i++) {
        Node fieldNode = Nodes.item(i);
        String name = fieldNode.getNodeName();
        if (name != null && name == fieldName) {
            return fieldNode;
        }//  w w w .j a v  a 2  s  .c  o m
    }
    return null;
}

From source file:Main.java

public static Element getElement(Element el, String tagName, int index) {
    NodeList list = el.getElementsByTagName(tagName);
    return (Element) list.item(index);
}

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();
        }//w  w  w . j av  a 2 s  .c  o m
    }
    return result;
}

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);
        if (nd.getNodeName().equals(name)) {
            resList.add(nd);// w w w. j a v  a  2  s  .com
        }
    }
    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: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 . ja  va 2s  . c  om

    }

    return null;
}