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 getNodeContents(Node parentNode) {
    StringBuilder sb = new StringBuilder();

    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        String nodeValue = node.getNodeValue();
        if (nodeValue != null) {
            sb.append(nodeValue);/*from   ww w.ja v a2  s.c om*/
        }
    }

    return sb.toString();
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<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.addElement(namespace + ":" + nodeName);
        }/* w w w .  j  a v  a  2s .c  om*/
    }
    return properties;
}

From source file:Main.java

public static Node findChildNode(Node parentNode, int type) {
    Node foundNode = null;/*  w  ww  .j a v  a 2s. co  m*/
    NodeList nl = parentNode.getChildNodes();
    int len_nl = nl.getLength();
    for (int j = 0; j < len_nl; j++) {
        foundNode = nl.item(j);
        if (foundNode.getNodeType() == type)
            return foundNode;
    }

    return null;
}

From source file:Main.java

public static List<Element> getElementsByTagName(Element element, String name) {

    List<Element> elements = new LinkedList<Element>();
    NodeList nodeList = element.getElementsByTagName(name);

    for (int i = 0; i < nodeList.getLength(); i++) {
        elements.add((Element) nodeList.item(i));
    }/*from   w ww  .  j  a va 2 s  .c o  m*/

    return elements;
}

From source file:Main.java

/**
 * Returns the first element matching the given tag name.
 * @param doc the document from which to search the element
 * @param tagName the name of the tag to match
 * @return the element or null if the element cannot be found
 *///from  www  .j av a 2s  . c om
public static Element getFirstElementByTagName(Document doc, String tagName) {
    NodeList nodes = doc.getElementsByTagName(tagName);
    if (nodes.getLength() > 0) {
        return (Element) nodes.item(0);
    }
    return null;
}

From source file:Main.java

public static Element selectSingleElement(Element parent, String elementName) {
    NodeList list = parent.getElementsByTagName(elementName);

    if (list.getLength() > 0) {
        return (Element) list.item(0);
    }// w  ww  .j  a  va 2  s.c o m

    return null;
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
 * Do we need this ?/* w w w. ja  va  2 s .  co  m*/
 * @param element Element
 * @param name String
 * @param caseSensitive boolean
 * @return Node
 */

public static Node getChildNodeByName(Node element, String name, boolean caseSensitive) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                return node;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static String[] getChildrenText(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<String> list = new LinkedList<String>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//from w ww .j  a  v  a 2 s  . c  o  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(getText((Element) n));
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:Main.java

/**
 * Return the first child element with a given name.  
 * @param parent parent element/*from  w  ww.j  a v  a2s  .  com*/
 * @param name child element name
 * @return 
 */
public static Element getFirstChildByName(Element parent, String name) {
    NodeList children = parent.getElementsByTagName(name);
    if (children.getLength() == 0) {
        return null;
    }
    return (Element) children.item(0);
}

From source file:Main.java

/**
 * Get the attribute values of a given name/value pair for
 * the first XML {@code org.w3c.dom.Element} of given name.
 *
 * @param elem the parent XML Element/*from   w  ww.j  a  va 2s.  co  m*/
 * @param name the name of the child text Element
 * @return attribute name and value Map of named child Element
 */
public static Map<String, String> getAllAttributes(Element elem, String name) {
    Map<String, String> attributes = new TreeMap<String, String>();
    NodeList nodeList = elem.getElementsByTagName(name);
    int length = nodeList.getLength();
    for (int n = 0; n < length; ++n) {
        attributes.put(((Element) nodeList.item(n)).getAttribute("name"),
                ((Element) nodeList.item(n)).getAttribute("value"));
    }
    return attributes;
}