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

public static void removeAllChildren(Element deps) {
    NodeList childNodes = deps.getChildNodes();

    for (int i = childNodes.getLength() - 1; i >= 0; i--) {
        Node item = childNodes.item(i);
        deps.removeChild(item);//  ww  w.  ja v  a2s. c o m
    }
}

From source file:Main.java

/**
 * Search a first node by name in the document
 * //from w ww  .  j  av  a 2 s.co m
 * @param doc      source document
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodesByName(Document doc, String nodeName) throws Exception {

    Node node = null;

    NodeList nl = doc.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            break;
        }
    }

    return node;

}

From source file:Main.java

public static List getChildren(Element parentEl, String name) {
    List children = new ArrayList();
    NodeList l = parentEl.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) {
            children.add((Element) n);
        }/*w  w  w. j a v a 2  s.  com*/
    }
    return children;
}

From source file:Main.java

public static Collection<Node> search_nodes_by_attribute(Node root, String attr_name, String attr_value) {
    Collection<Node> result = new LinkedList<Node>();
    if (root instanceof Element) {
        if (((Element) root).hasAttribute(attr_name)
                && ((Element) root).getAttribute(attr_name).equals(attr_value))
            result.add(root);/* ww  w  . ja  v a2 s  .c o  m*/
    }
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        Collection<Node> ret = search_nodes_by_attribute(child, attr_name, attr_value);
        result.addAll(ret);
    }
    return result;
}

From source file:Main.java

/**
 * Extracts the first CDATA child from the given {@code org.w3c.dom.Element}.
 *
 * @param elem the parent Element/*from w  w w  . jav a  2  s.  c o m*/
 * @return the String value of the CDATA section, or {@code null} if none
 *         found
 */
public static String getCdata(Element elem) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
            CharacterData cdataNode = (CharacterData) node;
            return cdataNode.getData();
        }
    }
    return null;
}

From source file:Main.java

public static void removeElementXML(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {/*from   ww w  .  j  a  v  a 2s  .  c  o m*/
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeElementXML(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static String getElementValue(Node node, String nodeName) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(nodeName)))
            return getElementValue(child);
    }// w ww.  j av  a 2s.co  m
    return null;
}

From source file:Main.java

public static Element[] getElementsByName(Element parent, String name) {

    List<Node> resList = Lists.newArrayList();
    NodeList nl = getNodeList(parent);
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(name)) {
            resList.add(nd);//from   w  w w .  j  av  a 2  s .c  o  m
        }
    }
    Element[] res = new Element[resList.size()];
    for (int i = 0; i < resList.size(); i++) {
        res[0] = (Element) resList.get(i);
    }

    return res;
}

From source file:Main.java

public static Element getElementByTagName(Element parent, String childName) {
    NodeList nl = parent.getElementsByTagName(childName);
    if (nl.getLength() == 0)
        return null;
    return (Element) nl.item(nl.getLength() - 1);
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search//from ww w .ja v a 2  s.  co  m
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementIgnoreCase(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equalsIgnoreCase(element)) {
                return n;
            }
        }
    }

    return null;
}