Example usage for org.w3c.dom Node getChildNodes

List of usage examples for org.w3c.dom Node getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Node getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

/**
 * Gets the nodenames of childs./*from   www.ja  va 2s . co  m*/
 *
 * @param dataNode
 *            the data node
 * @return the nodenames of childs
 */
public static List<String> getNodenamesOfChilds(Node dataNode) {
    List<String> returnList = new ArrayList<String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node.getNodeName());
        }
    }

    return returnList;
}

From source file:Main.java

public static Element getChildElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }// w  w w.j  a v a 2  s.co m
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null
                    && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                return (Element) item;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Searches parent node and returns first found matching node.
 *
 * @param node     The parent node//from   w w  w . ja  va2 s .  com
 * @param nodeName The child node's element name
 * @return The first matching child Node
 */
public static Node getNode(Node node, String nodeName) {
    NodeList ch = node.getChildNodes();
    int l = ch.getLength();
    for (int i = 0; i < l; i++) {
        Node n = ch.item(i);
        if (n.getNodeName().equals(nodeName)) {
            return n;
        }
    }
    return null;
}

From source file:Main.java

public static String getId(Node node) {
    try {// w  w w. j a v a2 s .  co  m
        NamedNodeMap nnm = node.getAttributes();
        Node attrib = nnm.getNamedItem("Id");
        Object ID;
        if (attrib.hasChildNodes()) {
            ID = attrib.getChildNodes().item(0).getNodeValue();
        } else {
            ID = attrib.getNodeValue();
        }
        return ID.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:Main.java

/**
 * Gets first child element with specified name.
 *
 * @param parentNode parent node./*from w  w  w .  j ava  2s.co  m*/
 * @param childName  child name.
 * @return first childr element with specified name.
 */
public static Element getChildElement(final Node parentNode, final String childName) {
    //        parentNode.normalize();
    final NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

public static String getSubTagValue(Element root, String tagName, String subTagName) {
    String returnString = "";
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null)
                        && child.getNodeName().equals(subTagName)) {
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null)
                        return grandChild.getNodeValue();
                }/*  www  .  j ava 2 s  .  c  o m*/
            } // end inner loop
        }
    }
    return returnString;
}

From source file:Main.java

public static Iterable<Node> getChildrenWithTagName(final Node node, String name) {
    List<Node> list = new ArrayList<>();
    for (Node n : iterate(node.getChildNodes())) {
        if (n.getNodeName().equals(name)) {
            list.add(n);/*from  w  ww .  java2 s .c o m*/
        }
    }
    return list;
}

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }//  w w  w .  j  a v  a2s  . c o  m
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}

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 ww  .j a  v a 2 s .c  om*/
    }
    return properties;
}

From source file:Main.java

public static Node getTextNode(Node node) {
    Node textnode = null;//w ww.  j a va 2 s.com
    NodeList nodes = node.getChildNodes();
    int len = nodes.getLength();
    if (len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.TEXT_NODE) {
                textnode = nodes.item(i);
                break;
            }
        }
    }
    return textnode;
}