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

StringgetElementTextOrNull(final XMLStreamReader reader)
Gets the element text or null if the element has no text.
if (reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
    throw new XMLStreamException("parser must be on START_ELEMENT to read next text", reader.getLocation()); 
int eventType = reader.next();
StringBuffer content = null;
while (eventType != XMLStreamConstants.END_ELEMENT) {
    if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
            || eventType == XMLStreamConstants.SPACE || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
...
StringgetElementTextValue(Element element)
Get the element text value.
String value = "";
Node node = getElementTextNode(element);
if (node != null) {
    value = node.getNodeValue();
String testValue = value.trim();
if (testValue.length() <= 0) {
    value = "";
...
StringgetElementTextValue(Element in)
retrieve text from text from element
Node[] testItems = getSubnodesOfType(in, Node.TEXT_NODE);
if (testItems.length > 0)
    return (testItems[0].getNodeValue());
testItems = getSubnodesOfType(in, Node.CDATA_SECTION_NODE);
if (testItems.length > 0)
    return (testItems[0].getNodeValue());
return (null);
StringgetSimpleElementText(Element element)
Returns the text inside an element.
StringBuffer buffer = new StringBuffer();
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child instanceof Text) {
        buffer.append(child.getNodeValue());
return buffer.toString();
StringgetSimpleElementText(Element node)
*

This function is intended for use when you have merely text between an XML Element (i.e.

StringBuffer sb = new StringBuffer();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child instanceof Text) {
        sb.append(child.getNodeValue());
return sb.toString();
StringgetSimpleElementText(Element node)
get Simple Element Text
if (node == null)
    return null;
StringBuffer sb = new StringBuffer();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child instanceof Text) {
        sb.append(child.getNodeValue());
...
StringgetSimpleElementText(Element node, boolean trim)
get Simple Element Text
if (node.getChildNodes().item(0) instanceof Text) {
    String elemTxt;
    if (trim) {
        elemTxt = node.getChildNodes().item(0).getNodeValue().trim();
    } else {
        elemTxt = node.getChildNodes().item(0).getNodeValue();
    return elemTxt;
...
StringgetSimpleElementText(final Element element, final String nodeName)
get Simple Element Text
final Element namedElement = getFirstElement(element, nodeName);
if (namedElement != null) {
    return getSimpleElementText(namedElement);
return null;