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 String getChildNodeText(Node root, String childName) {
    NodeList nodeList = root.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        if (childName.equals(nodeList.item(i).getNodeName()))
            return nodeList.item(i).getTextContent();
    }// ww w  .j a  v  a2s  .  c  o m
    return "";
}

From source file:Main.java

/**
 * Retrieves a Map of the value of child nodes in the form { [Node Name] =>
 * [Node Value] }//from w  ww .j a v a  2  s  .  c o m
 * 
 * @param node
 */
public static Map<String, String> getChildValueMap(Node node) {
    HashMap<String, String> map = new HashMap<String, String>();

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node n = children.item(i);
        String nodeName = n.getNodeName();
        String nodeValue = getNodeValue(n);

        map.put(nodeName, nodeValue);
    }

    return map;
}

From source file:Main.java

public static void removeNodesByName(Node node, String name) {
    NodeList nl = node.getChildNodes();

    int i = 0;/*from   w w w.j  a va2  s .c om*/
    while (i < nl.getLength())
        if (nl.item(i).getNodeName().equals(name))
            node.removeChild(nl.item(i));
        else
            i++;
}

From source file:Main.java

public static Collection<Element> getChilds(Element e) {
    List<Element> childs = new ArrayList<Element>();
    if (e == null)
        return childs;

    NodeList cnodes = e.getChildNodes();
    for (int i = 0; i < cnodes.getLength(); i++) {
        Node cnode = cnodes.item(i);
        if (cnode instanceof Element) {
            Element child = (Element) cnode;
            childs.add(child);/*from w ww.j  a  va 2 s .  c om*/
        }
    }
    return childs;
}

From source file:Main.java

public static List<Node> extractChildElements(Node parent) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            result.add(child);//from  w w  w. j  a  v a2  s .  c o m
        }
    }
    return result;
}

From source file:Main.java

public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) {
    NodeList nl = e.getChildNodes();
    Element result = null;/*from ww w. j  a v a 2s .c o  m*/
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (result != null) // more than one Element children 
                throw exc;
            result = (Element) n;
        }
    }

    if (result == null // zero Element children
            || !result.getTagName().equals(tag)) // wrong tag
        throw exc;
    return result;
}

From source file:Main.java

public static String getTagValue(Element root, String tagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            Node child = node.getFirstChild();
            if ((child != null) && (child.getNodeValue() != null)) {
                return child.getNodeValue();
            }//from w  ww  .j  a v a 2  s .com
        }
    }
    return returnString;
}

From source file:Main.java

public static Vector<String> getPropertiesFromXML(Node propNode) {
    Vector<String> properties;
    properties = new Vector<String>();
    NodeList childList = propNode.getChildNodes();

    for (int i = 0; i < childList.getLength(); i++) {
        Node currentNode = childList.item(i);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            String nodeName = currentNode.getLocalName();
            String namespace = currentNode.getNamespaceURI();
            // href is a live property which is handled differently
            properties.addElement(namespace + ":" + nodeName);
        }//w  w  w . j a  v a  2s.  c o  m
    }
    return properties;
}

From source file:Main.java

private static String getStringValue(Element element, String tagName) {
    String textValue = null;// w  ww.j av  a 2 s .  c o m
    NodeList nl = element.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textValue = el.getFirstChild().getNodeValue();
    }
    return textValue;
}

From source file:Main.java

/**
 * Removes all of the child nodes from a parent node.
 *///from   w  ww. j a  v  a  2s  . c  o m
public static void emptyNode(final Node parent) {
    final NodeList childNodes = parent.getChildNodes();
    for (int i = childNodes.getLength() - 1; i >= 0; i--) {
        final Node childNode = childNodes.item(i);
        childNode.getParentNode().removeChild(childNode);
    }
}