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

/**
 *
 * @param element Element//from   w  w  w  . j a v a  2s.  c o  m
 * @param namespace String
 * @param childName String
 * @return String
 */
public static String getChildStringNS(Element element, String namespace, String childName) {
    NodeList nodes = element.getElementsByTagNameNS(namespace, childName);
    if (nodes.getLength() > 0) {
        Node fnd = nodes.item(0).getFirstChild();
        if (fnd != null)
            return fnd.getNodeValue();
    }
    return "";
}

From source file:Main.java

public static List<Element> getElementsByTagName(Element element, String tagName) {
    ArrayList<Element> children = new ArrayList<Element>();
    if (element != null && tagName != null) {
        NodeList nodes = element.getElementsByTagName(tagName);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            children.add((Element) child);
        }//from   w w  w.  j a v  a2 s .c om
    }
    return children;
}

From source file:Main.java

public static Node getChildNodebyName(Node parentNode, String nameOfChildToFind) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node currentChildNode = childNodes.item(i);
        String childName = currentChildNode.getLocalName();
        if (childName != null)
            if (childName.equals(nameOfChildToFind))
                return currentChildNode;
    }/*from   w ww .  j  a va 2  s .c  o m*/
    return null;
}

From source file:Main.java

/**
 * Gets the sub nodes.//  w ww  .j  a  v a  2  s.  c  o  m
 *
 * @param dataNode
 *            the data node
 * @return the sub nodes
 */
public static List<Node> getSubNodes(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);
        returnList.add(node);
    }

    return returnList;
}

From source file:Main.java

/**
 * Method to convert a Node XML to a Element XML.
 * @param node node XML to input.//from  ww w.  j a  v  a2  s. c  o  m
 * @return elment XML.
 */
public static Element convertNodeToElement(Node node) {
    NodeList list = node.getChildNodes();
    Element m = null;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        //Node n = elem.getFirstChild();
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            m = (Element) n;
        }
    }
    return m;
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode/*w  w  w  .j a v a 2  s.c om*/
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

/**
 * Get textual content of a node(for text nodes only).
 * /*from www  . j av a2  s  . c  om*/
 * @param node a text node.
 * @return node's textual content.
 */
public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getTextContent();
    }
    return "";
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    if (nodeList.getLength() == 1) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }/*from w w  w  . j a  v  a 2 s  . co  m*/
    }
    return null;
}

From source file:Main.java

public static List<Element> getElementCollectionFromDocument(Document document, String tagName) {
    List<Element> res = new LinkedList<Element>();
    NodeList entries = document.getElementsByTagName(tagName);
    for (int i = 0; i < entries.getLength(); i++) {
        Node entry = entries.item(i);
        if (entry.getNodeType() == Node.ELEMENT_NODE) {
            res.add((Element) entry);
        }/*  w  ww  .jav a2s. co m*/
    }
    return res;
}

From source file:Main.java

public static List<Element> elements(Element element) {
    List<Element> elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            elements.add((Element) node);
        }//from w  w w.  j  av  a 2  s.c om
    }
    return elements;
}