get XML Element Node from Root Node - Android XML

Android examples for XML:XML Node

Description

get XML Element Node from Root Node

Demo Code

/*L//w  ww  .  j ava 2 s .  c o m
 *  Copyright SAIC, Ellumen and RSNA (CTP)
 *
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/national-biomedical-image-archive/LICENSE.txt for details.
 */
//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Element getElementNode(Node root, String nodeString) {
        NodeList nlist = root.getChildNodes();

        for (int i = 0; i < nlist.getLength(); i++) {
            if (nlist.item(i) instanceof Element) {
                if (nlist.item(i).getNodeName()
                        .equalsIgnoreCase(nodeString)) {
                    return (Element) nlist.item(i);
                }

                if (nlist.item(i).hasChildNodes()) {
                    Element retNode = getElementNode(nlist.item(i),
                            nodeString);

                    if (retNode != null) {
                        return retNode;
                    }
                }
            }
        }

        return null;
    }
}

Related Tutorials