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 getLastChild(Element parent, String name) {
    Element child = null;//from  ww  w  .  j av  a2s .  c  o  m
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            child = (Element) n;
        }
    }
    return child;
}

From source file:Main.java

public static Node getNodeByTagName(Element node, String name) {
    NodeList nodeList = node.getElementsByTagName(name);
    if (nodeList != null && nodeList.getLength() > 0)
        return nodeList.item(0);
    else//from w w  w. ja v  a 2s  . c  o  m
        return null;
}

From source file:Main.java

public static Element fetchSubElement(Element e, String subElementName) {
    if (e == null || subElementName == null) {
        return null;
    }/*  www  .j a v a  2  s .c o  m*/

    subElementName = subElementName.toUpperCase();

    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            Element element = (Element) list.item(i);
            if (element.getTagName().toUpperCase().equals(subElementName)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Node getFirstChildByTagName(Node parent, String tagName) {
    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))
            return node;
    }/*w  ww.  j a  va  2  s. com*/
    return null;
}

From source file:Main.java

public static Element getChildElementByTagName(Element ele, String childEleName) {

    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && childEleName.equals(node.getNodeName())
                || childEleName.equals(node.getLocalName())) {
            return (Element) node;
        }// w w w  .j  a  v a 2  s . c o  m
    }
    return null;
}

From source file:Main.java

public static Element getFirstChild(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            return (Element) n;
        }//from   w w  w.ja va  2s  .c  o  m
    }
    return null;
}

From source file:Main.java

public static List getChildrenElement(Node n, String childTagName) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);

        if ((cn.getNodeType() == Node.ELEMENT_NODE) && (cn.getNodeName().equals(childTagName))) {
            lst.add(cn);/* w  w  w  .  j a  va2  s . com*/
        }
    }
    return lst;
}

From source file:Main.java

public static List getChildrenElement(Node n) {
    List lst = new ArrayList();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node cn = nl.item(i);
        if (cn.getNodeType() == Node.ELEMENT_NODE) {
            lst.add(cn);//from  w  w w. j a  v a 2s  .co  m
        }
    }

    return lst;
}

From source file:Main.java

/**
 * Extract the text in an element.// w  ww  .j  a v  a2s.co  m
 * Concats data of Text and CDATASection elements.
 */
public static String getText(Element elem) {

    StringBuffer text = new StringBuffer();
    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Text)
            text.append(((Text) n).getData());
        else if (n instanceof CDATASection)
            text.append(((CDATASection) n).getData());
    }
    return text.toString();
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);/*from  ww w  . ja  v  a 2s  .co m*/
        }
    }
    return al.toArray(new Element[al.size()]);
}