get XML Element Contents as String - Android XML

Android examples for XML:XML Element

Description

get XML Element Contents as String

Demo Code

/*//from   w ww.  j ava 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.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static String getElementContents(Element element,
            String elementName) throws IOException {
        Element first = getFirstElement(element, elementName);
        if (first != null) {
            return first.getTextContent().trim();
        } else {
            return null;
        }
    }

    public static Element getFirstElement(Element element,
            String elementName) throws IOException {
        NodeList list = element.getElementsByTagName(elementName);
        if (list.getLength() >= 1) {
            Element subElement = (Element) list.item(0);
            return subElement;
        } else {
            return null;
        }
    }
}

Related Tutorials