Example usage for org.w3c.dom NodeList item

List of usage examples for org.w3c.dom NodeList item

Introduction

In this page you can find the example usage for org.w3c.dom NodeList item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the collection.

Usage

From source file:Main.java

static public String getStringValue(Element el, boolean trimWhitespace) {
    StringBuilder sb = new StringBuilder(1024);
    String str;//from w ww .  ja v a2 s  .  c o m

    NodeList nl = el.getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node n = nl.item(i);
        if (n instanceof Text) {
            sb.append(n.getNodeValue());
        }
    }
    if (trimWhitespace) {
        str = sb.toString().trim();
    } else {
        str = sb.toString();
    }
    return str;

}

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.
 * //ww  w. j  a va  2  s  .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 Node[] toNodeArray(NodeList nl) {
    Node[] res = new Node[nl.getLength()];
    for (int i = 0; i < res.length; i++) {
        res[i] = nl.item(i);
    }//from   w  ww. j a v a 2s  .c  om
    return res;
}

From source file:Main.java

protected static String getString(String tagName, Element rootElement) {
    NodeList list = rootElement.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        NodeList subList = list.item(0).getChildNodes();
        if (subList != null && subList.getLength() > 0) {
            return subList.item(0).getNodeValue();
        }/*from   www  .  ja v  a 2  s  .  c  o  m*/
    }

    return null;
}

From source file:Main.java

/**
 * Remove named nodes of the specified nodeType from the specified node.
 * //from  w ww  .ja  v  a2s  .com
 * @param node the node to be cleaned.
 * @param nodeType the type of nodes to be removed.
 * @param name the name of nodes to be removed.
 */
public static void removeAll(final Node node, final short nodeType, final String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {
        // Visit the children
        final NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static Node getChildNodeByName(Node node, String name) {
    if (node == null)
        return null;

    NodeList childs = node.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equalsIgnoreCase(name)) {
            return child;
        }//from   w w  w.j a v a2  s. c om

    }
    // not found
    return null;
}

From source file:Main.java

/**
 * Get the Element children of an element as an array.
 * @param element/*w  w  w  .  j  a  va  2  s .c  o m*/
 * @return non-<code>null</code> array 
 */
public static Element[] getChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();
    List<Element> kids = new ArrayList<Element>();
    Node node = childNodes.item(0);
    while (node != null) {
        if (node instanceof Element) {
            kids.add((Element) node);
        }
        node = node.getNextSibling();
    }
    return (Element[]) kids.toArray(new Element[kids.size()]);
}

From source file:Main.java

public static String getNodeAttr(String tagName, String attrName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if ((data.getNodeType() == Node.ATTRIBUTE_NODE)
                        && data.getNodeName().equalsIgnoreCase(attrName)) {
                    return data.getNodeValue();
                }//from w ww. ja  v  a2s. com
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Retrieves the first element, within the supplied element that has the specified tagName.
 * @param ele Parent element to search through
 * @param tagName Element name to find/*  w w  w  .  jav a  2s . c o  m*/
 * @return Element within the parent element that has the specfied name, or null.
 */
public static Element getDistinctElementByName(Element ele, String tagName) {
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        return (Element) nl.item(0);
    }

    return null;
}

From source file:Main.java

/**
 * Returns a List of all child Elements of the given Element.
 *
 * @param element the element from which to retrieve child elements
 * @return a List of child Elements./*from  www .j a va 2  s. com*/
 * @throws NullPointerException if the element is null.
 */
public static List getChildElements(Element element) {
    if (element == null)
        throw new NullPointerException("Tried to get child elements on a null element");

    List result = new ArrayList();

    NodeList children = element.getChildNodes();

    for (int i = 0; i != children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
            result.add(children.item(i));
        }
    }

    return result;
}