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:com.spun.util.ups.UPSUtils.java

/***********************************************************************/
private static Node getNodeByName(Node node, String childNode) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).getNodeName().equals(childNode)) {
            return list.item(i);
        }//from ww  w . j a  v a 2  s.  com
    }
    return null;
}

From source file:Main.java

public static List<Node> getAllChildNodes(Node node) {
    if (node == null)
        return null;
    List<Node> result = new ArrayList<Node>();
    NodeList nodelist = node.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node curnode = nodelist.item(i);
        int type = curnode.getNodeType();
        if (type != Node.TEXT_NODE)
            result.add(nodelist.item(i));
        List<Node> childlist = getAllChildNodes(curnode);
        if (childlist != null)
            result.addAll(childlist);/* w  w  w.j a  v  a 2 s .c  om*/
    }
    return result;
}

From source file:Main.java

/**
 * Find node by name recursively.//from  ww  w.j  a v a  2  s  . c om
 * @param node Node
 * @param nodeName String
 * @return Node
 */
public static Node findNode(Node node, String nodeName) {
    if (nodeName.equals(node.getNodeName()))
        return node;
    org.w3c.dom.NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node ret = findNode(children.item(i), nodeName);
        if (ret != null)
            return ret;
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first child element that matches the given local name and
 * namespace. If no child element is present or no child element matches,
 * <code>null</code> is returned.
 *
 * @param parent/*www .  ja  v a2s .  c om*/
 * @param childLocalName
 * @param childNamespaceURI
 * @return first child element matching the specified names or <code>null</code>.
 */
public static Element getChildElement(Node parent, String childLocalName, String childNamespaceURI) {
    if (parent != null) {
        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && childLocalName.equals(child.getLocalName())
                    && childNamespaceURI.equals(child.getNamespaceURI())) {
                return (Element) child;
            }
        }
    }
    return null;
}

From source file:Main.java

public static int getNodeValue(final Node parentNode, final String strNodeName, final int iDefaultValue) {
    final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null);

    if (strValue != null) {
        return Integer.parseInt(strValue);
    }// w ww . ja  va  2s  . c  om

    return iDefaultValue;
}

From source file:Main.java

private static String getSubTagValue(Node node, String subTagName) {
    String returnString = "";
    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 w  w  .  j a  va  2  s  .  c  o m*/
            }
        }
    }
    return returnString;
}

From source file:Main.java

public static long getNodeValue(final Node parentNode, final String strNodeName, final long lDefaultValue) {
    final String strValue = getNodeValue(parentNode.getChildNodes(), strNodeName, null);

    if (strValue != null) {
        return Long.parseLong(strValue);
    }/*from  w  w  w.j  a  v  a  2 s.  c om*/

    return lDefaultValue;
}

From source file:Main.java

/**
 * @return a list of all child nodes with this name
 *///www. j  a va2s  . c o  m
public static ArrayList<Node> getChildNodes(Node node, String name) {
    ArrayList<Node> results = new ArrayList<Node>();

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node temp = nodeList.item(i);
        if (temp.getNodeType() != Node.ELEMENT_NODE)
            continue;
        if (name.equals(temp.getLocalName()) || name.equals(temp.getNodeName())) {
            results.add(temp);
        }
    }

    return (results);
}

From source file:Main.java

public static String GetStringValueForNode(Node item) throws Exception {
    if (item instanceof Element) {
        StringBuilder builder = new StringBuilder();
        NodeList children = item.getChildNodes();
        for (int index = 0; index < children.getLength(); index++)
            builder.append(children.item(index).getNodeValue());

        // return...
        return builder.toString();
    } else/*from   ww  w .j  av  a  2  s .  c o m*/
        throw new Exception(String.format("Cannot handle '%s'.", item.getClass()));
}

From source file:Main.java

public static Node getNodeByName(Node node, String name) {
    if (node == null)
        return null;

    NodeList nl = node.getChildNodes();

    Node n = null;//from w  w  w.  j a va 2 s .  c o  m
    int i = 0;
    while (n == null && i < nl.getLength()) {
        if (nl.item(i).getNodeName().equals(name))
            n = nl.item(i);
        i++;
    }

    return n;
}