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

/**
 * Checks if the given node has only text children.
 *
 * @param node parent.//from  w  w  w. j  a  v a  2s.  c o  m
 * @return 'TRUE' if the given node has only text children; 'FALSE' otherwise.
 */
public static boolean hasOnlyTextChildNodes(final Node node) {
    boolean result = true;
    final NodeList children = node.getChildNodes();
    for (int i = 0; result && i < children.getLength(); i++) {
        final Node child = children.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            result = false;
        }
    }

    return result;
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }/*  w ww  . j a va 2 s .  co  m*/

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    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();
                    }/*from   w ww  . j a  va 2 s.c om*/
                }
            }
        }
    }
    return returnString;
}

From source file:Main.java

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 * /*  w w  w.  jav  a 2 s. c  o m*/
 * @param node
 *            The node to search from.
 * @param namespaceURI
 *            An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 *         is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (child.getNamespaceURI().equals(namespaceURI))
            list.add((Element) child);
    }
    return list;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode, String nodeName) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeName().equals(nodeName))
            ret.add((Element) children.item(i));
    }// www. jav a  2  s.  c  o m
    return ret;
}

From source file:Main.java

public static String getTagValue(String sTag, Element eElement) {
    if (eElement == null) {
        return null;
    }//  w  ww  .ja  va2 s.  com
    NodeList aNodeList = eElement.getElementsByTagName(sTag);
    if (aNodeList == null) {
        return null;
    }
    Node aNode = aNodeList.item(0);
    if (aNode == null) {
        return null;
    }
    NodeList nlList = aNode.getChildNodes();
    Node nValue = (Node) nlList.item(0);
    if (nValue == null) {
        return null;
    }
    return nValue.getNodeValue();
}

From source file:Main.java

/**
 * Searches below the supplied Node for a "list" tag and then retrieves the contents of the "value" tag(s) under that.
 * Note that if there is more than one "list" under the node then we will take the values from the first one only.
 * /*from  www  .  j  a  va2s .  c  o m*/
 * @param node
 *          acts as the root for the seach
 * 
 * @return list of Strings corresponding to the contents of the "value" tag(s)
 */
public static ArrayList getElementListValues(Node node) {
    ArrayList values = new ArrayList();

    // search for list tag
    Document doc = node.getOwnerDocument();
    NodeList list = doc.getElementsByTagName("list");

    if (list.getLength() == 0)
        return values;

    // search under that for value tag(s)
    doc = list.item(0).getOwnerDocument();
    NodeList vals = doc.getElementsByTagName("value");

    // for each one we get the text contents
    for (int i = 0; i < vals.getLength(); i++) {
        Node v = vals.item(i);
        NodeList text = v.getChildNodes();

        if (text == null) {
            values.add("");
            continue;
        }

        // there should be only text inside the value tag
        Node value = text.item(0);
        if (value == null)
            values.add("");
        else
            values.add(value.getNodeValue());
    }

    return values;
}

From source file:Main.java

/**
 * Retrieves a Map of the value of child nodes in the form { [Node Name] =>
 * [Node Value] }//from  w  w  w  .  j av  a2s. 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

/**
 * Returns a List of all descendant Element nodes having the specified
 * [namespace name] property. The elements are listed in document order.
 *
 * @param node The node to search from.//  www. j  a v  a  2  s .  c  o  m
 * @param namespaceURI An absolute URI denoting a namespace name.
 * @return A List containing elements in the specified namespace; the list
 * is empty if there are no elements in the namespace.
 */
public static List<Element> getElementsByNamespaceURI(Node node, String namespaceURI) {
    List<Element> list = new ArrayList<Element>();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (child.getNamespaceURI().equals(namespaceURI)) {
            list.add((Element) child);
        }
    }
    return list;
}

From source file:Main.java

public static int outputNode(Node outputNode, PrintWriter outputWriter, int curPos) {
    NodeList nodes = outputNode.getChildNodes();
    int curNodeNum;
    if (outputNode.getNodeType() == Node.TEXT_NODE) {
        outputWriter.print(outputNode.getNodeValue());
    } else {/*from   w w w . j av a 2 s.  c  o m*/
        if (outputNode.getNodeName().equals("p")) {
            outputWriter.println();
        }

        for (curNodeNum = 0; curNodeNum < nodes.getLength(); curNodeNum++) {
            Node curNode = nodes.item(curNodeNum);
            curPos = outputNode(curNode, outputWriter, curPos);
            //System.out.println(curNode.getNodeName());
            //System.out.println(curNode.getNodeValue());
        }

    }
    return (curPos);
}