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

Android examples for XML:XML 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 {
    /**//ww w . j ava 2 s. c om
     * 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();
        //System.out.println("NodeList length:" + l.getLength());
        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