Java Utililty Methods XML Element Text

List of utility methods to do XML Element Text

Description

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

Method

StringgetElementText(Element element, String tagName, String namespace)
get Element Text
Element e = getElement(element, tagName, namespace);
if (e != null) {
    String text = ((Element) e).getTextContent().trim();
    if (text != null) {
        text = text.trim();
        if (text.length() > 0) {
            return text;
return null;
StringgetElementText(Element elm)
get Element Text
Node node = elm.getFirstChild();
if (node != null && node.getNodeType() == Node.TEXT_NODE)
    return node.getNodeValue();
return null;
StringgetElementText(Element elm)
get Element Text
Node node = elm.getFirstChild();
if (node != null && node.getNodeType() == Node.TEXT_NODE) {
    return node.getNodeValue();
return null;
StringgetElementText(Element elm)
get Element Text
Node node = elm.getFirstChild();
if (node != null && node.getNodeType() == Node.TEXT_NODE)
    return node.getNodeValue();
return null;
StringgetElementText(Element n)
get Element Text
NodeList childNodes = n.getChildNodes();
String result = new String();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node node = childNodes.item(i);
    if (node.getNodeType() == Node.TEXT_NODE) {
        result += node.getNodeValue();
return result;
StringgetElementText(Element namedElement)
get Element Text
if (namedElement == null) {
    return null;
String text = namedElement.getTextContent();
return (text.isEmpty()) ? null : text;
StringgetElementText(Element root, String tagname)
This methods extracts the text for a named element under the root.
NodeList nameList = root.getElementsByTagName(tagname);
if (nameList.getLength() > 0) {
    Node text = nameList.item(0).getFirstChild();
    while ((text != null) && !(text instanceof Text)) {
        text = text.getNextSibling();
    if (text == null) {
        return null;
...
StringgetElementText(final Element element)
Returns the text within a specified element.
final StringBuffer elementText = new StringBuffer(256);
for (Node childNode = element.getFirstChild(); childNode != null; childNode = childNode.getNextSibling()) {
    if (childNode.getNodeType() == Node.TEXT_NODE) {
        final Text textNode = (Text) childNode;
        elementText.append(textNode.getData());
    } else if (childNode.getNodeType() == Node.CDATA_SECTION_NODE) {
        final CDATASection cdataNode = (CDATASection) childNode;
        elementText.append(cdataNode.getData());
...
StringgetElementTextByTagName(Element n, String elementName)
get Element Text By Tag Name
Element subEl = getElementByTagName(n, elementName);
if (subEl != null)
    return getElementText(subEl);
return null;
StringgetElementTextDataNoEx(Element element, boolean unindent)
get Element Text Data No Ex
String result = null;
try {
    result = getElementTextData(element);
    if (unindent)
        result = unindentTextData(result);
} catch (Exception e) {
return result;
...