get XML Node Child By Tag Name - Android XML

Android examples for XML:XML Child Element

Description

get XML Node Child By Tag Name

Demo Code


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

public class Main{
    public static Element getChildByTagName(Node node,
            String tagName)//from   w ww  .  ja  va  2s  .  c  om
    /*    */{
        /* 27 */if (node.getNodeType() != 1) {
            /* 28 */throw new RuntimeException(tagName
                    + "?????????,???Element???");
            /*    */}
        /* 30 */Element el = (Element) node;
        /*    */
        /* 32 */NodeList nodeList = el.getElementsByTagName(tagName);
        /* 33 */int length = nodeList.getLength();
        /*    */
        /* 35 */if ((nodeList == null) || (length == 0)) {
            /* 36 */return null;
            /*    */}
        /* 38 */return (Element) nodeList.item(0);
        /*    */}
    public static Element getChildByTagName(Document doc,
            String tagName)
    /*    */{
        /* 51 */NodeList nodeList = doc.getElementsByTagName(tagName);
        /* 52 */int length = nodeList.getLength();
        /*    */
        /* 54 */if ((nodeList == null) || (length == 0)) {
            /* 55 */return null;
            /*    */}
        /* 57 */return (Element) nodeList.item(0);
        /*    */}
}

Related Tutorials