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<Element> getElementCollectionByTagName(Element element, String tagName) {
    List<Element> result = new LinkedList<Element>();
    NodeList nodeList = element.getElementsByTagName(tagName);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            result.add((Element) node);
        }/*from  w w  w .  ja  v  a  2 s. co  m*/
    }
    return result;
}

From source file:Utils.java

/**
 * <p>Returns the first child element with the given name. Returns
 * <code>null</code> if not found.</p>
 *
 * @param parent parent element/*w w  w.  j  av a 2s  . c  o  m*/
 * @param name name of the child element
 * @return child element
 */
public static Element getChildElementByName(Element parent, String name) {
    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element getGrandSonElementByTagName(Element element, String parentName, String eleName) {

    NodeList nl = element.getElementsByTagName(parentName);
    if (null == nl) {
        return null;
    }//from  w  w w. ja  va2 s.co  m
    Node item = nl.item(0);
    return getChildElementByTagName((Element) item, eleName);
}

From source file:Main.java

public static void removeTextNodes(Node node) {
    NodeList nl = node.getChildNodes();

    int i = 0;//from  w ww .  ja v a  2  s .c o  m
    while (i < nl.getLength())
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            node.removeChild(nl.item(i));
        else
            i++;
}

From source file:Main.java

public static Element getFirstElement(Element element, String elementName) throws IOException {
    NodeList list = element.getElementsByTagName(elementName);
    if (list.getLength() >= 1) {
        Element subElement = (Element) list.item(0);
        return subElement;
    } else {/*from  www.  j av  a2s .  c om*/
        return null;
    }
}

From source file:Main.java

static Element findChild(Element element, String ns, String tag) {
    final NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            Element child = (Element) childNodes.item(i);
            if (child.getLocalName().equals(tag) && (ns == null || child.getNamespaceURI().equals(ns))) {
                return child;
            }//from  w  w  w .j a  va  2  s . co  m
        }
    }
    return null;
}

From source file:Main.java

public static final List<Element> getChildElements(Element parent) {
    final List<Element> childElements = new ArrayList<Element>();

    final NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        final Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }//w ww  .  ja  v a 2  s.c o m
    }

    return childElements;
}

From source file:Main.java

public static List<Element> getElementsByTagNames(Element element, String[] tagNames) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tagNames != null) {
        List tagList = Arrays.asList(tagNames);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/*from   w  w w  . j a v a2 s  . c  om*/
        }
    }
    return children;
}

From source file:Main.java

public static String getTextValue(Element e) {
    NodeList childs = e.getChildNodes();
    for (int k = 0; k < childs.getLength(); k++) {
        Node n = childs.item(k);
        if (n.getNodeType() == 3) {
            return n.getNodeValue();
        }/* w  w w. ja  v  a  2  s .  c om*/
    }
    return null;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    if (node == null) {
        return null;
    }/*from  w  w  w . jav  a 2  s .com*/

    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();
        }
    }
    return null;
}