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

/**
 * returns a XML element./*  w  w w.j  av a 2 s .c om*/
 *
 * @param pDocument         Document XML DOM document
 * @param psTagName         String XML node name
 * @param index             int XML node position
 * @return                  Element XML node element
 */
public static Element getElement(Document pDocument, String psTagName, int index) {
    NodeList rows = pDocument.getDocumentElement().getElementsByTagName(psTagName);
    return (Element) rows.item(index);
}

From source file:Main.java

/**
 * Returns the first Element node in a NodeList
 * @param list the list to search//from w w  w.  j  ava2s  .co  m
 * @return the element or null if none is found
 */
public static Element getFirstElement(NodeList list) {
    if (list.getLength() == 0) {
        return null;
    }

    Node node = list.item(0);
    while (node.getNodeType() != Node.ELEMENT_NODE) {
        if (node.getNextSibling() == null) {
            return null;
        }
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static Collection<Node> search_nodes_by_name(Node root, String name) {
    Collection<Node> result = new LinkedList<Node>();
    if (root.getNodeName().equals(name))
        result.add(root);//from   w w  w  .ja va 2 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_name(child, name);
        result.addAll(ret);
    }
    return result;
}

From source file:Main.java

public static List<Element> getRealChilds(Node node) {
    List<Element> interlayer = new ArrayList<Element>();
    NodeList l = node.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Element) {
            interlayer.add(((Element) n));
        }// w  w  w. ja v a 2  s  .  co m
    }
    return interlayer;
}

From source file:Main.java

public static Element getElementByTagName(Document document, String element) {
    NodeList nodeList = document.getElementsByTagName(element);
    if (nodeList.getLength() > 0) {
        return (Element) nodeList.item(0);
    } else {/*from   ww  w  . j  a v  a  2  s  .c o  m*/
        return null;
    }
}

From source file:Main.java

public static void removeChilds(Node node) {
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        node.removeChild(nl.item(i));
}

From source file:Main.java

/**
 * Set the value of element/*from   w  w w . j  av a2  s  . c  o m*/
 *
 * @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

public static Node getChildNodeByName(Node node, String childName) {
    NodeList children = node.getChildNodes();
    for (int j = 0; j < children.getLength(); j++) {
        Node child = children.item(j);
        if (child.getNodeName().equals(childName)) {
            return child;
        }//  w  ww . j a va 2s  .  c  om
    }
    return null;
}

From source file:Main.java

public static Node get_child(Node parent, String child_name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            return child;
    }//w w w. j a v  a  2s  . c  om
    return null;
}

From source file:Main.java

public static Node getFirstChild(Node node, String childName) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child.getNodeName().equals(childName)) {
            return child;
        }/* w w  w  .  ja  v  a2  s  .  c  o m*/
    }
    return null;
}