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 String getElementText(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return ((Text) child).getNodeValue();
        }//from w  ww. j  a va  2s  .c  o m
    }
    return "";
}

From source file:Main.java

/**
 * Returns the first node that matches the specified string.
 * //from w  w w . jav a 2  s  .c  o  m
 * @param d
 * @param s
 * @return Node object of the first node that matches name
 */
public static Node getFirstNodeByName(Document d, String s) {
    NodeList ns = d.getElementsByTagName(s);
    Node n = null;
    if (ns.getLength() > 0) {
        n = ns.item(0);
    }
    return n;
}

From source file:Main.java

/**
 * This method gets the child node with the provided name from the 
 * provided start node.// w  w  w . j  a v  a  2s.c om
 * @param start The Parent node to scan children.
 * @param nodeName The name of the node that we're looking for.
 * @return The Node with name: nodeName, whose parent is the start node.
 *
 * @author <a href='mailto:intere@gmail.com'>Eric Internicola</a>
 */
public static Node getChildNode(Node start, String nodeName) {
    Node child = null;

    NodeList children = start.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);

        if (nodeName.equals(n.getNodeName())) {
            child = n;
            break;
        }
    }

    return child;
}

From source file:Main.java

public static int getCountOfChildNodes(Node node, String nodeName) {
    int count = 0;

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

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                count++;/*w w  w.j  av a  2  s .c  o  m*/
            }
        }

    }

    return count;
}

From source file:Main.java

public static List<Node> getChildNodeList(Node parentNode, String nodeName) {
    List<Node> childNodeList = new ArrayList<>();
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (((Element) childNode).getNodeName().equals(nodeName)) {
                childNodeList.add(childNode);
            }/*from  w w w. ja  v  a 2 s.c  o m*/
        }
    }
    return childNodeList;
}

From source file:Main.java

public static String getElementText(final Element eElement, final String name) {
    NodeList nodes = eElement.getElementsByTagName(name);
    if (nodes == null) {
        return null;
    }/*from   www  . j  a va  2  s. com*/
    Node node = nodes.item(0);
    if (node == null) {
        return null;
    }
    String value = node.getTextContent();
    return (value != null) ? value.trim() : null;
}

From source file:Main.java

static public 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.getNodeValue();
        }/* w  w w . j a v  a2  s .c  o m*/
    }
    return "";
}

From source file:Main.java

/**
 * Find node by name.//w  w w.  ja v a  2s.  co m
 * @param node Node
 * @param nodeName String
 * @return Node
 */
public static Node getChildByName(Node node, String nodeName) {
    org.w3c.dom.NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeName().equals(nodeName))
            return c;
    }
    return null;
}

From source file:Main.java

public static Iterator getElementsByTagNames(Element element, String[] tags) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tags != null) {
        List tagList = Arrays.asList(tags);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }//from  ww  w  . j a va  2 s . c o  m
        }
    }
    return children.iterator();
}

From source file:Main.java

/**
 * Get all child elements// w w  w  . ja v  a2 s .co m
 * @param ele parent element
 * @return list of child elements
 */
public static List<Element> getChildElements(Element ele) {
    List<Element> list = new ArrayList<>();
    NodeList childNodes = ele.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) childNodes.item(i));
        }
    }
    return list;
}