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 Element getChildElement(Element elem, String name) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            return (Element) node;
        }/*from   ww  w.  j a  v  a  2  s.co  m*/
    }
    return null;
}

From source file:Main.java

public static Node getChildNode(String name, Node node) {
    if (node == null) {
        return null;
    }//from   ww  w  . j a  v  a  2  s . c  om
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node no = list.item(i);
        if (no.getNodeName().equalsIgnoreCase(name)) {
            return no;
        }
    }
    return null;
}

From source file:Main.java

public static final Element getElementByTagName(Element parent, String name) throws IOException {
    final NodeList list = parent.getElementsByTagName(name);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    } else {//from w w  w. j  a  v  a 2s.  co m
        throw new IOException(String.format("Expected one child element named \"%s\"", name));
    }
}

From source file:Main.java

/**
 * Find node by node while ignoring the case.
 * /*  w w  w.j  a  v  a2s . c  om*/
 * @param tagName     node tag (case-insensitive).
 * @param nodes       a list of nodes to look through.
 * @return a node name if the node is found, or null otherwise.
 */
public static Node getNode(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            return node;
        }
    }

    return null;
}

From source file:Main.java

public static void printNodeList(NodeList list) {
    for (int i = 0; i < list.getLength(); i++) {
        printNode(list.item(i), "");
        System.out.println("############################");
    }/*  w  w w.java  2s. c o  m*/
}

From source file:Main.java

public static Element getElement(Element parentNode, String nodeName) {
    if (parentNode == null) {
        return null;
    }/*from  w  ww .j av a 2  s  .  c  o m*/
    NodeList nl = parentNode.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            if (nl.item(i).getNodeName().equals(nodeName)) {
                return (Element) nl.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getValueByNodeName(Node parentNode, String nodeName) {
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        if (nodeList.item(i).getNodeName().equalsIgnoreCase(nodeName)) {
            return nodeList.item(i).getTextContent();
        }/*w  ww . j ava2s.  co  m*/
    }
    return null;
}

From source file:Main.java

public static Element findElement(Element parent, String uri, String localName) {
    NodeList nl = parent.getElementsByTagNameNS(uri, localName);
    if (nl.getLength() > 0) {
        return (Element) nl.item(0);
    } else {//  ww  w  . j a  v  a 2s . c  o m
        return null;
    }
}

From source file:Main.java

/**
 * Set the value of the first tag found with the given name in the given document<br/>. If the tag with the
 * requested name doesn't have a child as textnode with the tagValue is created.
 *
 * @param doc/*w ww. j  a va 2 s  .c  o  m*/
 * @param tagName
 * @param tagValue
 */
public static void setTagValue(Document doc, String tagName, String tagValue) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        if (tagList.item(0).getFirstChild() != null) {
            tagList.item(0).getFirstChild().setNodeValue(tagValue);
        } else {
            tagList.item(0).appendChild(doc.createTextNode(tagValue));
        }
    }
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " begin...");
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);//from w  w w  .j a  v a  2s  .com
            logger.debug("remove child '" + nd + "' success.");
        }
    }
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " end.");
}