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

/**
 * @param node/*w w w  . j  a v  a2  s .c om*/
 * @return
 */
public static Element getFirstElement(Node node) {
    NodeList children = node.getChildNodes();
    if (children == null || children.getLength() == 0) {
        return null;
    }
    int len = children.getLength();
    for (int i = 0; i < len; i++) {
        Node n = children.item(i);
        if (n instanceof Element) {
            return (Element) n;
        }
    }
    return null;
}

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++;/*ww  w  . j a  v  a  2 s  .  co m*/
            }
        }

    }

    return count;
}

From source file:Main.java

public static Element getFirstChildElement(Element element) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            return (Element) node;
        }/*from   w w w . j ava 2s. c  o m*/
    }
    return null;
}

From source file:Main.java

/**
 * This is a helper function to deal with problems that occur when importing Nodes from
 * JTidy Documents to Xerces Documents./*from w w w .  j  av a2  s .  c o m*/
 */
public static Node importNode(Document d, Node n, boolean deep) {
    Node r = cloneNode(d, n);
    if (deep) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n1 = importNode(d, nl.item(i), deep);
            r.appendChild(n1);
        }
    }
    return r;
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);/*w  w w  . j  a  v  a2 s.  com*/
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}

From source file:Main.java

static public List<Element> getChildElementList(Element root) {
    List<Element> list = new ArrayList<>();
    NodeList nodes = root.getChildNodes();
    int i, size = nodes.getLength();
    Node node;// w  w  w. ja  v  a2s  .  c  o  m

    for (i = 0; i < size; i++) {
        node = nodes.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE)
            list.add((Element) node);
    }

    return list;
}

From source file:Main.java

public static void getElementsByTagName(Node parent, String tagName, List<Node> result) {
    if (parent == null) {
        return;/*from   www  .j  a v  a2 s.  c o  m*/
    }

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            result.add(child);
        } else if (child.hasChildNodes()) {
            getElementsByTagName(child, tagName, result);
        }
    }
}

From source file:Main.java

public static Node getTag(Element root, String tagName) {
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType());
        if (node.getNodeName().equals(tagName)) {
            return node;
        }/*  w  w  w  .  ja v  a2  s.  c  o  m*/
    }
    return null;
}

From source file:Main.java

public static Node findChildWithTagName(Node parent, String tagName) {
    if (parent == null) {
        return null;
    }//from   w  ww .  j  a va  2  s.c  om

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            return child;
        } else if (child.hasChildNodes()) {
            Node result = findChildWithTagName(child, tagName);
            if (result != null) {
                return result;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * /*from   w  ww.j a v a 2  s.co m*/
 * @param element
 * @param nodeName
 * @return Element
 */
public static Element getFirstElement(final Element element, final String nodeName) {
    final NodeList list = element.getElementsByTagName(nodeName);

    if (list.getLength() == 0) {
        return null;
    }

    return (Element) list.item(0);
}