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 elem, String name)
get Element Text
NodeList nl = elem.getElementsByTagName(name);
if (nl.getLength() > 0)
    return getText((Element) nl.item(0));
else
    return null;
StringgetElementText(Element element)
get Element Text
return element.getTextContent();
StringgetElementText(Element element)
Returns the text content of a DOM Element.
NodeList children = element.getChildNodes();
int childCount = children.getLength();
for (int index = 0; index < childCount; ++index) {
    if (children.item(index) instanceof Text) {
        Text text = (Text) children.item(index);
        return text.getData();
return null;
StringgetElementText(Element element)
get Element Text
StringBuffer value = new StringBuffer();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node instanceof Text || node instanceof CDATASection) {
        value.append(node.getNodeValue());
return value.toString();
StringgetElementText(Element element)
get Element Text
for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
return element.getNodeValue();
StringgetElementText(Element element)
Returns the text of the element
StringBuffer buffer = new StringBuffer();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node node = nodeList.item(i);
    if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
        buffer.append(node.getNodeValue());
return buffer.toString();
StringgetElementText(Element element)
Return the contained text within an Element.
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node c = nl.item(i);
    if (c instanceof Text) {
        return ((Text) c).getData();
return null;
...
StringgetElementText(Element element)
Returns the text content of the element.
StringBuilder builder = new StringBuilder(10);
for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (n instanceof Text) {
        builder.append(((CharacterData) n).getData());
return builder.toString();
StringgetElementText(Element element)
get Element Text
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.TEXT_NODE) {
        return ((Text) child).getNodeValue();
return "";
...
StringgetElementText(Element element)
get Element Text
for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
return element.getNodeValue();