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 List<Element> getChildren(Element element, String tagName) {
    List<Element> children = new ArrayList<>();
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (tagName != null && child.getTagName().equals(tagName))
                children.add(child);//  w  w  w. ja  v  a 2  s. c o  m
        }
    }
    return children;
}

From source file:Main.java

/**
 * Set the value of element/*  w w w. ja  v  a  2 s  . com*/
 *
 * @param element
 * @param val
 */
public static void setElementValue(Element element, String val) {
    Node node = element.getOwnerDocument().createTextNode(val);
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeType() == Node.TEXT_NODE) {
            nd.setNodeValue(val);
            return;
        }
    }
    element.appendChild(node);
}

From source file:Main.java

/**
 * Fetch node using its name/*from w ww . j  a v  a 2  s.  c  o  m*/
 * 
 * @param node
 * @param name
 * @return
 */
public static Node fetchByName(Node node, String name) {
    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node child = list.item(i);
        if (child.getNodeName().equals(name)) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);// w  w  w. j a va  2 s. c  o  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[al.size()]);
}

From source file:Main.java

public static List<Element> getChildElements(Element element) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            result.add((Element) childNodes.item(i));
        }//from  www. j  a  v  a  2  s  . c  o m
    }
    return result;
}

From source file:Main.java

private static Node actualFindNode(Node node, String name) {

    String nodeName = node.getNodeName();
    nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));

    if (nodeName.equals(name)) {
        return node;
    }// w  w  w.j  a v  a 2 s .co  m
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();

        for (int i = 0; i < size; i++) {
            Node found = actualFindNode(list.item(i), name);
            if (found != null)
                return found;
        }
    }
    return null;
}

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;
    }/* w w  w .  ja v a  2  s  .c o  m*/
    return null;
}

From source file:Main.java

public static List<Node> getValidChildNodes(Node node) {
    List<Node> nl = new ArrayList<>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        if (isValidNode(nodeList.item(i)))
            nl.add(nodeList.item(i));/*from ww w. jav  a2  s  . c  o  m*/
    }
    return nl;
}

From source file:Main.java

public static Double get_double(Element element, String name) {
    Double d = null;/* ww w  . j  a  v a 2  s  .com*/
    NodeList list = element.getElementsByTagName(name);
    if (list.getLength() > 0) {
        try {
            d = Double.valueOf(list.item(0).getTextContent());
        } catch (NumberFormatException e) {
            System.err.println(e);
            return null;
        }
    } else {
        System.err.println("No \"" + name + "\" defined");
        return null;
    }

    return d;
}

From source file:Main.java

/**
 * Gets the first child Element with a given name
 *
 * @param n the node get the children from
 * @param elementName the name of the child elements
 * @return the first child Element with a given name
 *///from  w ww .  j a v  a  2 s.c o m
public static Element getElementByTagName(Element n, String elementName) {
    NodeList subNodes = n.getElementsByTagName(elementName);
    int sz = subNodes.getLength();
    if (sz > 0) {
        return (Element) subNodes.item(0);
    }
    return null;
}