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 getChild(Element element, String fieldName) {
    if ((element == null) || (fieldName == null) || fieldName.equals("")) {
        return null;
    }// w  ww.  ja  va2  s .  com
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(fieldName)) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Fetch node using its name//  w w w . j av  a  2 s.  c  o  m
 * 
 * @param node
 * @param name
 * @return
 */
public static Node fetchByName(Node node, String name) {
    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node child = list.item(i);
        if (child.getNodeName().equals(name)) {
            return child;
        }
    }
    return null;
}

From source file:Main.java

public static Node getChildNode(Node node, String childName) {
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node curNode = nodes.item(i);
        if (curNode.getLocalName() != null && curNode.getLocalName().equals(childName)) {
            return curNode;
        }/* www .j  av a 2  s  .  com*/
    }
    return null;
}

From source file:Main.java

/**
 * Retrieve the value of an XML tag. For internal use only during parsing the accounts
 * configuration file./*from w w w  .j  av  a2 s.  co m*/
 *
 * @param element The elemement containing the tag
 * @param tagName The string name of the tag
 * @return Tag value
 */
public static String getTagValue(Element element, String tagName) throws Exception {
    try {
        NodeList tagList = element.getElementsByTagName(tagName);
        Element tagElement = (Element) tagList.item(0);
        NodeList textTagList = tagElement.getChildNodes();

        return (textTagList.item(0)).getNodeValue().trim();
    } catch (Exception e) {
        throw new Exception(
                "Error in parsing the element \"" + element.toString() + "\" with the tag \"" + tagName + "\"");
    }
}

From source file:Main.java

public static Element get_child_with_attr(Node parent, String child_name, String attr_name, String attr_value) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name)) {
            if (child instanceof Element) {
                Element e = (Element) child;
                if (e.getAttribute(attr_name).equals(attr_value))
                    return e;
            }// ww  w . j  a va  2  s. c om
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode, String nodeName) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeName().equals(nodeName))
            ret.add((Element) children.item(i));
    }//w ww . j  av  a2s . com
    return ret;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element)
            ret.add((Element) children.item(i));
    }/*from www.  j  a  v  a  2 s  .  c o  m*/
    return ret;
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
 * Do we need this ?/*from  ww w  .java2 s. c  om*/
 * @param element Element
 * @param name String
 * @param caseSensitive boolean
 * @return Node
 */

public static Node getChildNodeByName(Node element, String name, boolean caseSensitive) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                return node;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return node;
        }
    }
    return null;
}

From source file:Main.java

public static Node getChildElement(Node parent, String elementName) {
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        String nodeName = childNode.getLocalName();
        if (elementName.equals(nodeName)) {
            return childNode;
        }//w ww  . j a v  a 2  s.co  m
    }
    return null;
}

From source file:Main.java

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