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

static private boolean existNode(Node node, String nodeName, String textValue) {
    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        if (childs.item(i).getNodeName().equals(nodeName)
                && childs.item(i).getTextContent().equals(textValue)) {
            return true;
        }// ww  w.  j  a  v  a 2 s.  c o  m
    }
    return false;
}

From source file:Main.java

public static String getTextContent(Node node) {

    if (node == null)
        throw new NullPointerException();

    String textContent;/*  ww  w.j av  a 2 s  .  c o  m*/

    textContent = node.getTextContent();
    if (textContent != null)
        return xmlDecode(textContent);

    NodeList childNodes = node.getChildNodes();
    if (childNodes.getLength() > 0 && childNodes.item(0) instanceof Text)
        textContent = node.getNodeValue();
    if (textContent != null)
        return xmlDecode(textContent);

    return "";
}

From source file:Main.java

/**
 *   Gets the text for a node by concatenating child TEXT elements.
 *//*  ww w .  j  ava2 s.  c o  m*/
public synchronized static String getText(Node n) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");

    StringBuilder b = new StringBuilder();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            b.append(nl.item(i).getNodeValue());
        else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
            b.append(nl.item(i).getNodeValue());
    }

    return b.toString().trim();
}

From source file:Main.java

public static void forEach(NodeList list, Consumer<Node> consumer) {
    for (int i = 0; i < list.getLength(); i++) {
        consumer.accept(list.item(i));
    }/*from w  w w . j a v  a2 s.  c om*/
}

From source file:Main.java

public static void forEach(NodeList nl, Consumer<Node> action) {
    for (int i = 0; i < nl.getLength(); i++) {
        action.accept(nl.item(i));
    }//from   w ww .  j ava 2 s.c  o m
}

From source file:Main.java

public static String getElementValueByTagName(Element root, String tagName) {
    NodeList nodes = root.getElementsByTagName(tagName);
    if (nodes != null) {
        Node node = nodes.item(0).getFirstChild();
        if (node != null) {
            return node.getNodeValue();
        } else {//from   www . j  a v  a  2  s. c  om
            return null;
        }
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Returns the children element of an XML node
 *//*from  ww  w .j a v  a  2s. c  om*/
public static Iterable<Element> children(Node n) {
    final List<Element> e = new ArrayList<Element>();
    final NodeList ns = n.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        if (ns.item(i) instanceof Element) {
            e.add((Element) ns.item(i));
        }
    }
    return e;
}

From source file:Main.java

public static void printNodes(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        printNode(nodes.item(i));
    }/* w  w  w  .  j  a  v a2  s.c o  m*/

}

From source file:Main.java

public static String getStringValueForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);
    if (nl.getLength() > 0) {
        Node node = nl.item(0).getFirstChild();
        if (node != null) {
            String output = node.getNodeValue().trim();
            return output;
        }//from   www  . j  a  v a2 s  .c  om
    }
    return "";
}

From source file:Main.java

public static Node findNodeByTagName(Document document, String tagName) {
    Node foundNode = null;/*  w  ww  . j  a v  a2  s.c  om*/
    NodeList nodes = document.getElementsByTagName(tagName);
    if (nodes.getLength() > 0)
        foundNode = nodes.item(0);
    return foundNode;
}