Java Utililty Methods XML Child Element Text

List of utility methods to do XML Child Element Text

Description

The list of methods to do XML Child Element Text are organized into topic(s).

Method

StringgetChildText(Element tag, String childTagName)
get Child Text
Element child = getFirstChild(tag, childTagName);
if (child != null) {
    return getNodeText(child);
return ""; 
StringgetChildText(final Element element, final String tagName)
get Child Text
final Element childElement = getChild(element, tagName);
if (childElement == null) {
    return null;
final String text = childElement.getTextContent();
return (text == null) ? null : text.trim();
StringgetChildText(final Element element, final String tagName)
Obtains the text value inside the first child element with the specified tag name.
final Element child = getChildElement(element, tagName);
return getText(child);
StringgetChildText(final Element parentElem, final String childName)
Gets the text content of a child element.
NodeList childList = parentElem.getElementsByTagName(childName);
if (childList.getLength() > 0) {
    Node child = childList.item(0);
    return child.getTextContent();
} else {
    return null;
StringgetChildText(final Node node)
Returns the concatenated child text of the specified node.
if (node == null) {
    return null;
StringBuffer str = new StringBuffer();
Node child = node.getFirstChild();
while (child != null) {
    short type = child.getNodeType();
    if (type == Node.TEXT_NODE) {
...
StringgetChildText(final Node node)
get Child Text
if (node == null) {
    return null;
} else {
    final StringBuilder text = new StringBuilder();
    appendChildText(text, node);
    return text.toString();
StringgetChildText(Node node)
Returns the value of the first child node of the given node, and asserts that the child node is a Text node.
Node child = node.getFirstChild();
assert (child.getNodeType() == Node.TEXT_NODE);
return child.getNodeValue();
StringgetChildText(Node node)
We get a object that connected text node of the child node and the CDATA section as character string.
StringBuffer buf = new StringBuffer();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
    Node n = node.getChildNodes().item(i);
    if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
        buf.append(n.getNodeValue());
return buf.toString();
...
StringgetChildText(Node node)
Returns the concatenated child text of the specified node.
if (node == null) {
    return null;
StringBuffer str = new StringBuffer();
Node child = node.getFirstChild();
while (child != null) {
    short type = child.getNodeType();
    if (type == Node.TEXT_NODE) {
...
StringgetChildText(Node parent, String childName)
Returns the text value of a given child element.
Element elem = findFirstChild(parent, childName);
if (elem == null)
    return null; 
return getElementText(elem);