go recursively deep to find the XML node with the name - Java XML

Java examples for XML:DOM Node

Description

go recursively deep to find the XML node with the name

Demo Code


//package com.java2s;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*from   w  w w  .  j  a  va  2  s .  c  o  m*/
     * this function will go recursively deep to find the node with the name
     * @param n
     * @param name
     * @return
     */
    public static Node getNodeByName(Node n, String name) {
        NodeList l = n.getChildNodes();
        if (l == null)
            return null;

        for (int i = 0; i < l.getLength(); i++) {
            if (l.item(i).getNodeName().equals(name))
                return l.item(i);
            else {
                Node n2 = getNodeByName(l.item(i), name);
                if (n2 != null) {
                    return n2;
                }
            }
        }
        return null;
    }
}

Related Tutorials