return the content of this particular XML element. - Android XML

Android examples for XML:XML Element

Description

return the content of this particular XML element.

Demo Code

/*/*w  w w.  ja v  a 2 s .  c om*/
 * Copyright (c) 2002-2006 by OpenSymphony
 * All rights reserved.
 */
//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * This method will return the content of this particular <code>element</code>.
     * For example,
     *
     * <pre>
     *    <result>something_1</result>
     * </pre>
     * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
     * argument (<code>element</code> to this method, it returns the content of it,
     * namely, <code>something_1</code> in the example above.
     * 
     * @return
     */
    public static String getContent(Element element) {
        StringBuffer paramValue = new StringBuffer();
        NodeList childNodes = element.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
            Node currentNode = childNodes.item(j);
            if (currentNode != null
                    && currentNode.getNodeType() == Node.TEXT_NODE) {
                String val = currentNode.getNodeValue();
                if (val != null) {
                    paramValue.append(val.trim());
                }
            }
        }
        String val = paramValue.toString().trim();
        return val;
    }
}

Related Tutorials