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 List<Node> getDescendents(Node node, String nodeName) throws IOException {
    List<Node> childList = new ArrayList<Node>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeName().equals(nodeName)) {
            childList.add(child);//from ww w  . j ava  2 s  . c o m
        }
    }
    return childList;
}

From source file:Main.java

public static String getTextContent(Element elm) {
    if (elm == null) {
        return null;
    }/*from  w  w w.j  ava 2  s. c o  m*/
    StringBuilder result = new StringBuilder();
    NodeList childNodes = elm.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if ((node == null) || (node.getNodeType() != Node.TEXT_NODE)) {
            continue;
        }
        result.append(((Text) node).getData());
    }
    return result.toString().trim();
}

From source file:Main.java

/**
 * Get the first instance of an element by name.
 *
 * @param parent The parent to get the element from.
 * @param elementName The name of the element to look for.
 * @return The element or null if it is not found.
 *///from ww w. j  a v  a 2s  .co  m
public static Element getElement(Element parent, String elementName) {
    Element retval = null;
    NodeList children = parent.getElementsByTagName(elementName);
    if (children.getLength() > 0) {
        retval = (Element) children.item(0);
    }
    return retval;
}

From source file:Main.java

public static Element getChildElement(Element elt, String name) {
    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(node.getNodeName()))
                return (Element) node;
    }/* w ww .  j av a2s  .  co  m*/
    return null;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    List<Element> ret = new ArrayList<Element>();
    NodeList childList = parent.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element child = (Element) childList.item(i);
        ret.add(child);//from   w  ww  .ja va2s  .  c  om
    }
    return ret;
}

From source file:Main.java

public static Element find(Element element, String name) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(name)) {
            return (Element) child;
        }/*from  ww  w.  ja  va 2  s . co m*/
    }
    return null;
}

From source file:Main.java

public static Node findNode(Node parentNode, String key) {
    NodeList childNodes = parentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeName().equals(key)) {
            return childNode;
        }/*  w w  w .ja va2s. co m*/
    }
    return null;
}

From source file:Main.java

public static final List<Element> getChildElements(Element element) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) item);
        }//from ww w.  j  av  a2 s. com
    }
    return childElements;
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuffer sb = new StringBuffer();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();/*from  w w w.  java2  s.  c  o  m*/
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static List<Element> getElements(Element parent, String tagName) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodes = parent.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            elements.add(Element.class.cast(node));
        }//from  w  w  w  .ja v  a  2  s . c  o m
    }
    return elements;
}