Java XML Child Element Text getChildText(Element parent, String childName)

Here you can find the source of getChildText(Element parent, String childName)

Description

Get the text content of a given child name.

License

Open Source License

Parameter

Parameter Description
parent Parent Element
childName Tag name of the child element

Return

Text content of a child element with the given tag name with the parent element. null if no child element is found. null or empty string if child element is found, but does not have text content.

Declaration

public static final String getChildText(Element parent, String childName) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*from   w  w  w  .  j  a  v a  2  s .c  om*/
     * Get the text content of a given child name.
     * 
     * @param parent
     *            Parent Element
     * @param childName
     *            Tag name of the child element
     * 
     * @return Text content of a child element with the given tag name with the parent element. null if no child element
     *         is found. null or empty string if child element is found, but does not have text content.
     */
    public static final String getChildText(Element parent, String childName) {
        Element childElement = getFirstLevelChildElementByTagName(parent, childName);

        if (childElement != null) {
            return getText(childElement);
        }

        return null;
    }

    /**
     * Get the immediate child element with a given tag name. If multiple child elements exist with the same tag name,
     * the first child element is returned.
     * 
     * @param parent
     *            Parent Element
     * @param elementName
     *            Tag name of the child elements to be returned.
     * 
     * @return First child element which has the given tag name.
     */
    public static final Element getFirstLevelChildElementByTagName(Element parent, String elementName) {
        Node childNode = parent.getFirstChild();
        while (childNode != null) {
            if ((childNode.getNodeType() == Node.ELEMENT_NODE)
                    && ((Element) childNode).getLocalName().equals(elementName)) {
                return (Element) childNode;
            }
            childNode = childNode.getNextSibling();
        }
        return null;
    }

    /**
     * Get Text Content of the given element.
     * 
     * @param elem
     *            Element
     * 
     * @return Trimmed text content of the element.
     */
    public static final String getText(Element elem) {
        if (elem != null) {
            NodeList childNodes = elem.getChildNodes();
            for (int i = 0; i < childNodes.getLength(); i++) {
                if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                    return trim(childNodes.item(i).getNodeValue());
                }
            }
        }

        return null;
    }

    private static String trim(String input) {
        if (input == null) {
            return input;
        }

        return input.trim();

    }
}

Related

  1. getChildText(Element element, String nodeName)
  2. getChildText(Element element, String tag)
  3. getChildText(Element element, String tag)
  4. getChildText(Element parent, String childLocalName, String childNamespaceURI)
  5. getChildText(Element parent, String childName)
  6. getChildText(Element parent, String childName)
  7. getChildText(Element parent, String childName)
  8. getChildText(Element parent, String kidName)
  9. getChildText(Element parent, String name)