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

public static Element[] getChildElementNodes(Node node) {
    if (node == null) {
        return null;
    }//  w  ww .j  av  a  2  s.  c  o m

    ArrayList<Element> elements = new ArrayList<>();
    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 && item.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) item);
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

public static int getValue(final Node node, final String strAttrName, final String strAttrValue,
        final int iDefaultValue) {
    final String strValue = getValue(node.getChildNodes(), strAttrName, strAttrValue);

    if (strValue == null) {
        return iDefaultValue;
    }/*from ww  w .  j  a va2s.com*/

    return Integer.parseInt(strValue);
}

From source file:Main.java

public static Node[] getChildNodes(final Node node, final String elementName) {
    final ArrayList nodeList = new ArrayList();
    final NodeList childs = node.getChildNodes();
    String nodeName = null;//w w w . ja  va2  s .  co  m
    for (int i = 0; i < childs.getLength(); i++) {
        nodeName = childs.item(i).getNodeName();
        if (nodeName != null && nodeName.equals(elementName)) {
            nodeList.add(childs.item(i));
        }
    } //next child node
    final Node[] result = new Node[nodeList.size()];
    nodeList.toArray(result);
    return result;
}

From source file:Main.java

/**
 * Helper Method. Searches through the child nodes of a node and returns the first node with a matching name.
 * @param xnode the node to get the child node from
 * @param name the name of the child node to search for
 * @param caseSensitive boolean true for case sensitive name matching, false otherwise
 * @return the located node or null if one was not found
 *///from w  w w  . j a va 2s  . c o m

public static Node getChildNodeByName(final Node xnode, final String name, final boolean caseSensitive) {
    NodeList list = xnode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (caseSensitive) {
            if (node.getNodeName().equals(name))
                return node;
        } else {
            if (node.getNodeName().equalsIgnoreCase(name))
                return node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * returns a XML node value.// w  w  w  .j a va 2s. c om
 *
 * @param pNode             Document XML node
 * @return                  String XML node value
 */
public static String getValue(Node pNode) throws Exception {
    String s = null;
    try {
        NodeList nodes = pNode.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            s = ((Node) nodes.item(i)).getNodeValue().trim();
            if (s.equals("") || s.equals("\r"))
                continue;
        }
    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }
    return s;
}

From source file:Main.java

public static Map<String, String> getChildElementNodesMap(Node node) {
    if (node == null) {
        return null;
    }// w ww .j a va 2 s. c  o m

    Map<String, String> map = new ConcurrentHashMap<>();
    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 && item.getNodeType() == Node.ELEMENT_NODE) {
                map.put(item.getNodeName(), item.getTextContent().trim());
            }
        }
    }
    return map;
}

From source file:Main.java

public static double getValue(final Node node, final String strAttrName, final String strAttrValue,
        final double dDefaultValue) {
    final String strValue = getValue(node.getChildNodes(), strAttrName, strAttrValue);

    if (strValue == null) {
        return dDefaultValue;
    }//  ww w. j a  v a 2s .co m

    return Double.parseDouble(strValue);
}

From source file:Main.java

private static List<Element> find(Node root, String nsUri, String localName, boolean recursive) {
    List<Element> result = new ArrayList<Element>();
    NodeList nl = root.getChildNodes();
    if (nl != null) {
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node child = nl.item(i);
            if (child instanceof Element) {
                String childUri = child.getNamespaceURI();
                String childLocalName = child.getLocalName();
                if (("*".equals(nsUri) || nsUri == null && childUri == null || nsUri.equals(childUri))
                        && localName.equals(childLocalName)) {
                    result.add((Element) child);
                } else if (recursive) {
                    result.addAll(find(child, nsUri, localName, recursive));
                }/*from   ww w . j a  va 2s.  c  o  m*/
            }
        }
    }
    return result;
}

From source file:Main.java

/**
 * Method selectNodeByName.//from  w ww  .ja  v  a2 s . c o m
 *
 * @param node
 * @param nodeName
 * @return Node
 *         <p/>
 *         Purpose: given a node & tag name, returns a single node that matches.
 *         Useful when it is known that a particular node will only have a single
 *         node of a particular tag name.
 */
public static Node selectNodeByName(Node node, String nodeName) {
    Node nodeToReturn = null;
    int i = 0;
    // get all the child nodes
    NodeList currNodes = node.getChildNodes();
    int length = currNodes.getLength();
    // search for an occurence that matches nodeName provided
    while ((i < length) && (nodeToReturn == null)) {
        Node thisNode = currNodes.item(i);
        if (thisNode.getNodeName().equals(nodeName)) {
            nodeToReturn = thisNode;
        }
        i++;
    }
    return nodeToReturn;
}

From source file:Main.java

/**
 * Finds the node of the argument name in the tree under the argument node.
 * @param name The name of the node to search for, case insensitive.
 * @param node The root node of the tree under which to look for the named node.
 * @return the first found occurrence of the named node (breadth first),
 * or null if the node is not found.// w w w  .  j  av  a  2 s.  c  o m
 */
public static Node findChild(String name, Node node) {
    if (node == null)
        return null;

    if (node.hasChildNodes()) {
        NodeList kids = node.getChildNodes();
        int numKids = kids.getLength();
        for (int index = 0; index < numKids; ++index) {
            Node kid = kids.item(index);
            if (kid.getNodeName().equalsIgnoreCase(name))
                return kid;
        } // for

        for (int index = 0; index < numKids; ++index) {
            Node found = findChild(name, kids.item(index));
            if (found != null)
                return found;
        } // for
    } // if

    return null;
}