Java XML Child Element Text getChildText(Element element, String defaultValue)

Here you can find the source of getChildText(Element element, String defaultValue)

Description

Returns the child text of this element, or the default value if there is none.

License

Open Source License

Parameter

Parameter Description
element a parameter
defaultValue a parameter

Return

Either the child text of the element or the default value if there is not child text.

Declaration

public static String getChildText(Element element, String defaultValue) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//  w  ww. j  a  va  2 s. com
     * Returns the child text of this element, or null if there is none.
     * @param element
     * @return String
     */
    public static String getChildText(Element element) {
        return getChildText(element, null);
    }

    /**
     * Returns the child text of this element, or the default value if there is none.
     * @param element
     * @param defaultValue
     * @return Either the child text of the element or the default value if there is not child text.
     */
    public static String getChildText(Element element, String defaultValue) {
        Node childNode = element.getFirstChild();
        if (childNode == null || !(childNode instanceof Text))
            return defaultValue;

        return childNode.getNodeValue();
    }
}

Related

  1. getChildElementValue(Element parentElm, String elementName, String defaultValue)
  2. getChildElementValueByTagName(Element ele, String childEleName)
  3. getChildElementValueByTagName(Element ele, String childEleName)
  4. getChildElementValueByTagName(Element parentElement, String childTag)
  5. getChildText(Element elem, String childTagName)
  6. getChildText(Element element, String nodeName)
  7. getChildText(Element element, String tag)
  8. getChildText(Element element, String tag)
  9. getChildText(Element parent, String childLocalName, String childNamespaceURI)