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<Node> getChildNodes(Node node, String... elements) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        for (String element : elements) {
            if (element.equals(childNode.getNodeName())) {
                result.add(childNode);//from  w ww . ja  v a2  s  .c o  m
            }
        }
    }
    return result;
}

From source file:Main.java

public static Element getChildByTagName(Element element, String tagName) {
    Element result = null;//from   w  w w.  j  a  v a2s.c o  m
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n = childNodes.item(i);
        if (!(n instanceof Element)) {
            continue;
        }
        if (((Element) n).getTagName().equals(tagName)) {
            if (result != null) {
                throw new RuntimeException("Too many elements with tag name " + tagName);
            }
            result = (Element) n;
        }
    }
    return result;
}

From source file:Main.java

/** Return a list of the elements that are direct children of the given
 * node./*w ww  . j a va 2  s. com*/
 */
public static List<Element> getChildElements(Node node) {
    NodeList childNodes = node.getChildNodes();
    List result = new ArrayList(childNodes.getLength());
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node oneChild = childNodes.item(i);
        if (oneChild instanceof Element)
            result.add(oneChild);
    }
    return result;
}

From source file:Main.java

public static Element getChildElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }/*from www  .j  a  v a  2 s  .c  om*/
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null
                    && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                return (Element) item;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Finds the first child of a node with the given name
 * @param parent the parent node/*from   w  w  w  . ja  v a2 s .c om*/
 * @param name the name
 * @return the child node
 */
public static Node findFirstChild(Node parent, String name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

private static void makeNamelist(Document doc) {
    String names = null;//from   w  ww. ja v a2 s . c om
    Element root = doc.getDocumentElement();
    NodeList nameElements = root.getElementsByTagName("name");
    for (int i = 0; i < nameElements.getLength(); i++) {
        Element name = (Element) nameElements.item(i);
        Text nametext = (Text) name.getFirstChild();
        if (names == null) {
            names = nametext.getData();
        } else {
            names += ", " + nametext.getData();
        }
    }
    Element namelist = doc.createElement("names");
    Text namelisttext = doc.createTextNode(names);
    namelist.appendChild(namelisttext);
    root.insertBefore(namelist, root.getFirstChild());
}

From source file:Main.java

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 * /*from  w  w w. j a  v  a  2s  .  c o  m*/
 * @param node
 *            The node to search from.
 * @param namespaceURI
 *            An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 *         is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (child.getNamespaceURI().equals(namespaceURI))
            list.add((Element) child);
    }
    return list;
}

From source file:Main.java

public static List<String> getPropertiesFromXML(Node propNode) {
    ArrayList<String> properties;
    properties = new ArrayList<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.add(namespace + ":" + nodeName);
        }/*  w  w w  .  j  a v  a2s  .co m*/
    }
    return properties;
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode//from  w w w.java  2 s .c om
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

public static Element getElementByTagNameAndAttributeValue(Element element, String tagName, String attrName,
        String attrValue) {/* w ww. ja v  a 2 s .c  o m*/
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element nodeElement = (Element) node;
            if (attrValue.equals(getAttributeValueByName(nodeElement, attrName))) {
                return nodeElement;
            }
        }
    }
    return null;
}