Android XML Element Child Get getChildValue(Element element, String name)

Here you can find the source of getChildValue(Element element, String name)

Description

Get the value of the fist child element with the given name.

Parameter

Parameter Description
element The parent element
name The child element name

Return

The child element value or null

Declaration

public static String getChildValue(Element element, String name) 

Method Source Code

//package com.java2s;

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

import org.w3c.dom.Text;

public class Main {
    /**//from w  w w. j a  va2  s .  co  m
     * Get the value of the fist child element with the given name.
     *
     * @param element The parent element
     * @param name The child element name
     * @return The child element value or null
     */
    public static String getChildValue(Element element, String name) {
        return getValue(getChild(element, name));
    }

    /**
     * Get the text value for the specified element.  If the element is null, or the element's body is empty then this
     * method will return null.
     *
     * @param element The Element
     * @return The value String or null
     */
    public static String getValue(Element element) {
        if (element != null) {
            Node dataNode = element.getFirstChild();
            if (dataNode != null) {
                return ((Text) dataNode).getData();
            }
        }
        return null;
    }

    /**
     * Get the first child element with the given name.
     *
     * @param element The parent element
     * @param name The child element name
     * @return The child element or null
     */
    public static Element getChild(Element element, String name) {
        return (Element) element.getElementsByTagName(name).item(0);
    }
}

Related

  1. findFirstChildElement(Element parent)
  2. firstChildWithName(Element element, String name)
  3. getChild(Element element, String name)
  4. getChildByType(Element element, short nodeType)
  5. getChildElementByName(Element parent, String name)
  6. getFullTextChildrenFromElement(Element element)
  7. printChildElements(Element root, PrintStream out, boolean recurse, String prefix)
  8. valueOfFirstChildWithName(Element element, String name)
  9. attributeOfFirstChildWithName(Element element, String name, String attribute)