A method to get XML Node from the child node by providing child's node name - Java XML

Java examples for XML:XML Element Child

Description

A method to get XML Node from the child node by providing child's node name

Demo Code


//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**/*w w  w .  j  av  a2 s .  co  m*/
     * A method to get Node from the child node by providing child's node name
     * @param Node parent, a node to search the child nodes 
     * @param String nodeName, name of the child node to get
     * @return Node , provided child node name
     */
    static public Node getChildNode(Node parent, String nodeName) {
        Node ret = null;

        Node n = parent.getFirstChild();
        while (n != null) {
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (n.getNodeName().trim().equals(nodeName)) {
                    ret = n;
                    break;
                }
            }
            n = n.getNextSibling();
        }
        return (ret);
    }
}

Related Tutorials