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 GetFirstInstance(NodeList nList) {
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null)
                return eElement.getTextContent();
        } // end if nnode.getnodetype()
    } //end for int temp
    return new String("");
}

From source file:Main.java

public static void removeNodeListContent(NodeList nodeList) {
    while (nodeList.getLength() > 0) {
        removeNode(nodeList.item(0));/*  w ww. ja  v a  2  s .  c  om*/
    }
}

From source file:Main.java

private static Node[] convertToArray(NodeList e) {

    int nodeSize = e.getLength();

    Node[] output = new Node[nodeSize];
    Node tempo = null;/*  w w w  .jav a  2 s .c  om*/

    for (int i = 0; i < nodeSize; i++) {

        tempo = e.item(i);

        output[i] = tempo;
    }

    return output;
}

From source file:Main.java

private static String getTextValue(Element ele, String tagName) {
    String textVal = null;//w  w  w  . j a va  2 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

/**
 * Returns the first node in the node list that is an element.
 * /*from   www. j a va2  s .  c om*/
 * @param nodes The list of nodes that contains the element to be
 *              determined.
 * 
 * @return The first element in the given node list.
 */
public static Node getFirstElement(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }//from  www .  j a v  a  2s.  com
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:Main.java

/**
 * // w  w w.j av a  2 s .c  om
 * @param nodeList
 * @return zda obsahuje elementy
 */
public static boolean containsElements(NodeList nodeList) {
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node instanceof Element) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static String getFirstElementAttr(Element parent, String tag, String attr) {
    NodeList nl = parent.getElementsByTagName(tag);
    if (nl.getLength() > 0) {
        return ((Element) nl.item(0)).getAttribute(attr);
    }// ww w.j  a  v a  2 s  . c  o m
    return null;
}

From source file:Main.java

public static void removeChildren(Node e) {
    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);/*from w w w  . j  a  va  2s  .c  o  m*/
        e.removeChild(n);
    }
}

From source file:Main.java

public static Node getRootNode(Document doc) {
    NodeList l = doc.getChildNodes();
    if (l != null && l.getLength() == 1)
        return l.item(0);

    return null;/* w w  w  . j  a  va 2  s.c  o  m*/
}