get Descendent Text from XML Node - Android XML

Android examples for XML:XML Node

Description

get Descendent Text from XML Node

Demo Code

/*/*from   w w  w .  j  a v a  2s .c  o  m*/
 * XmlUtil.java
 *
 * (c) 2009  The Echo Nest
 * See "license.txt" for terms
 *
 *
 */
//package com.java2s;
import java.io.IOException;

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

public class Main {
    public static String getDescendentText(Node node, String name)
            throws IOException {
        Node d = getDescendent(node, name);
        if (d != null) {
            return d.getTextContent().trim();
        }
        return null;
    }

    public static Node getDescendent(Node node, String nodeName)
            throws IOException {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeName().equals(nodeName)) {
                return child;
            }
        }
        return null;
    }
}

Related Tutorials