Java Utililty Methods XML Child Node Get

List of utility methods to do XML Child Node Get

Description

The list of methods to do XML Child Node Get are organized into topic(s).

Method

CollectiongetChildNodes(Node node)
get Child Nodes
Collection<Node> list = new LinkedList<Node>();
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE)
        list.add(nl.item(i));
return list;
Node[]getChildNodes(Node node, short type)
get Child Nodes
NodeList children = node.getChildNodes();
if (children == null) {
    return new Node[0];
int n = children.getLength();
List<Node> elnodelist = new ArrayList<Node>(n);
for (int i = 0; i < n; ++i) {
    Node childnode = children.item(i);
...
ArrayListgetChildNodes(Node node, String name)
get Child Nodes
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);
...
ListgetChildNodes(Node parent, String childName)
get Child Nodes
final List<Element> result = new ArrayList<Element>();
final NodeList nodes = parent.getChildNodes();
final int len = nodes.getLength();
for (int i = 0; i < len; i++) {
    final Node n = nodes.item(i);
    if (!isElementNode(n)) {
        continue;
    if (n.getNodeName().equals(childName)) {
        result.add((Element) n);
return result;
ArrayListgetChildNodes(Node parentNode)
This method will get the child nodes of a given node and return them in an ArrayList instead of a NodeList.
ArrayList<Node> childNodesArray = new ArrayList<Node>();
if (null != parentNode) {
    NodeList childNodes = parentNode.getChildNodes();
    if (null != childNodes && childNodes.getLength() > 0) {
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (null != child) {
                childNodesArray.add(child);
...
SetgetChildNodes(Node parentNode, String childName)
get Child Nodes
Set retVal = new HashSet();
NodeList children = parentNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node node = children.item(i);
    if (node.getNodeName().equalsIgnoreCase(childName)) {
        retVal.add(node);
return (retVal);
Node[]getChildNodes(Node parentNode, String childNodeName)
Returns an array of all child nodes of parentNode with the given node name or an empty list if no such child nodes exist.
Vector children = new Vector();
NodeList nodes = parentNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node n = nodes.item(i);
    if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.COMMENT_NODE)
        continue;
    if (childNodeName == null || n.getNodeName().equals(childNodeName))
        children.add(n);
...
Node[]getChildNodes(Node rootNode, String childName)
Returns all child nodes of the given root node with the given name.
List<Node> nodes = new ArrayList<Node>();
NodeList nodeList = rootNode.getChildNodes();
for (int nodeIndex = 0, nodeSize = nodeList.getLength(); nodeIndex < nodeSize; nodeIndex++) {
    Node childNode = nodeList.item(nodeIndex);
    if (childNode.getNodeName().equals(childName)) {
        nodes.add(childNode);
return nodes.toArray(new Node[nodes.size()]);
Node[]getChildNodesByName(Element parent, String name)
get Child Nodes By Name
List<Node> nodeList = new ArrayList<Node>();
NodeList childNodes = parent.getChildNodes();
int length = childNodes.getLength();
for (int i = 0; i < length; i++) {
    Node current = childNodes.item(i);
    if (current.getNodeName().equals(name))
        nodeList.add(current);
Node[] nodes = new Node[nodeList.size()];
nodeList.toArray(nodes);
return nodes;
ListgetChildNodesByName(Node element, String name, boolean caseSensitive)
Helper Method.
ArrayList<Node> nodes = new ArrayList<Node>();
NodeList list = element.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if (caseSensitive) {
        if (node.getNodeName().equals(name))
            nodes.add(node);
    } else {
...