Get Element from XML Root Element - Android XML

Android examples for XML:XML Element

Description

Get Element from XML Root Element

Demo Code


//package com.java2s;

import org.w3c.dom.*;

public class Main {
    public static Element GetElement(Element root, String namespaceUri,
            String name, boolean throwIfNotFound) throws Exception {
        NodeList nodes = root.getElementsByTagNameNS(namespaceUri, name);
        if (nodes.getLength() > 0)
            return (Element) nodes.item(0);
        else {/*from   www.j a  v  a2 s  .  c o m*/
            if (throwIfNotFound)
                throw new Exception(
                        String.format(
                                "A node with name '%s' in namespace '%s' was not found.",
                                name, namespaceUri));
            else
                return null;
        }
    }
}

Related Tutorials