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

/**
 * get single element by tag name/*from  w ww  . java  2s .  c o m*/
 * @param element
 * @param tag
 * @return
 */
public static Element getElementByTagName(Element element, String tag) {
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element && ((Element) node).getTagName().equals(tag)) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i)
        if (children.item(i) instanceof Element)
            return (Element) children.item(i);
    return null;/*from ww w .  ja  v  a2 s.  co m*/
}

From source file:Main.java

/**
 * Returns a node's first child node that is an element.
 * @param parent/*from w w w. ja  v  a 2  s  .c om*/
 * @return first child element, or null
 */
public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
    }
    return null;
}

From source file:Main.java

public static String getElementValue(Element element, String tagName, Integer n) {
    NodeList elementLst = element.getElementsByTagName(tagName);
    if (elementLst.item(n) != null) {
        NodeList elementData = ((Element) elementLst.item(n)).getChildNodes();
        if (elementData.item(0) != null) {
            return ((Node) elementData.item(0)).getNodeValue();
        }//from   w  w  w.j  a va  2s  .  com
    }
    return "";
}

From source file:Main.java

private static HashMap<String, String> getValoresNodo(Node nodo) {
    HashMap<String, String> valores = new HashMap<String, String>();
    NodeList listaNodo = nodo.getChildNodes();
    for (int i = 0; i < listaNodo.getLength(); i++) {
        Node n = listaNodo.item(i);
        valores.put(n.getNodeName(), getValorNodo(n));
    }//from ww w.  j  a v  a2  s  .  c  o m
    return valores;
}

From source file:Main.java

public static List<Element> getChildElements(Element element) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            result.add((Element) childNodes.item(i));
        }//w w w.j  ava  2s . com
    }
    return result;
}

From source file:Main.java

public static String getText(Node node) {
    String text = "";
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            text += child.getNodeValue() + " ";
        }/*from   ww w.j  a  v  a 2s .c o m*/
    }
    return text;
}

From source file:Main.java

static private Node getNode(Document doc, String nodeName, String textValue) {
    NodeList nodelist = doc.getElementsByTagName(nodeName);
    for (int i = 0; i < nodelist.getLength(); i++) {
        if (nodelist.item(i).getTextContent().equals(textValue)) {
            return nodelist.item(i);
        }// ww w . ja  v  a  2  s  .  c om
    }
    return null;
}

From source file:Utils.java

/**
 * Get the first child element// w  ww .  j  a v  a2 s.  c  om
 * @param parent
 * @return
 */
public static Element getFirstChildElement(Node parent) {
    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child instanceof Element) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static String getSingleValue(Element e, String s) {
    String value = null;//from   w  w w.j  av  a2  s  .c o  m
    NodeList nodeList = e.getElementsByTagName(s);
    if (nodeList.getLength() > 0) {
        Element _e = (Element) nodeList.item(0);
        NodeList eValue = _e.getChildNodes();
        if (eValue.getLength() > 0) {
            value = eValue.item(0).getNodeValue();
        }
    }

    return value;
}