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 the first child of a node with a specified name.
 * /*  w  w w.j  ava2 s . co  m*/
 * @param node The parent node.
 * @param name The name of the child.
 * @return The first child of <code>node</code> named <code>name</code>.
 */
public static Node getChildByName(Node node, String name) {
    try {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i).getNodeName().equals(name)) {
                return list.item(i);
            }
        }
        return null;

    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * Search a child node by type/*from ww  w  .  jav  a2s . co  m*/
 * 
 * @param parent   the parent node
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByType(Node parent, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals("Node")) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}

From source file:Utils.java

/**
 * //from  w  ww .j  a v  a  2s  .  c om
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static Element getChildByAttrName(Node node, String attrName, String attrValue) {

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) n;
            if (attrValue.equals(el.getAttribute(attrName))) {
                return el;
            }/*from  ww w  . ja v  a 2s . c o  m*/
        }
    }

    return null;
}

From source file:Main.java

public static Element getElementByTagName(Document doc, String eleName) {

    NodeList nl = doc.getElementsByTagName(eleName);
    if (null == nl) {
        return null;
    }/*from   w ww. j av a  2s .co  m*/
    Node item = nl.item(0);
    return (Element) item;
}

From source file:Main.java

public static Element getChildElement(String tagName, Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            return (Element) n;
        }//from ww w.j  a  v a2s  . c  o m
    }
    return null;
}

From source file:Main.java

public static String get_inner_text(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            return child.getNodeValue();
        if (child instanceof Text)
            return ((Text) child).getData();
        /*if (child instanceof TextNode)
           return ((TextNode)child).getData();*/
    }/*from w w w .j a  va  2  s .  c om*/
    return null;
}

From source file:Main.java

public static boolean isElementNodeExist(Node root, String nodeString) {
    NodeList nlist = root.getChildNodes();

    for (int i = 0; i < nlist.getLength(); i++) {
        if (nlist.item(i) instanceof Element) {
            if (nlist.item(i).getNodeName().equalsIgnoreCase(nodeString)) {
                return true;
            }/*from   www  .  j a va2 s.co  m*/

            if (nlist.item(i).hasChildNodes() && isElementNodeExist(nlist.item(i), nodeString)) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

public static Element findChildElement(Element parent, String name) {
    Element result = null;/* www  .j  ava2s . c om*/
    NodeList nodes = parent.getChildNodes();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);

        if (node.getNodeName().equals(name)) {
            result = (Element) node;
            break;
        }
    }

    return result;
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node parent, String tagName) {
    List<Node> eleList = new ArrayList<Node>();
    NodeList nodeList = parent.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equalsIgnoreCase(tagName)) {
            eleList.add(node);//from  w w w .  ja v a2 s  .com
        }

    }
    return eleList;
}