Java Utililty Methods XML Node Text Value

List of utility methods to do XML Node Text Value

Description

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

Method

StringgetTextContent(Node node, String path)
Get the text content of an element identified by a path, where the path elements can include an index.
if (node instanceof Document)
    node = ((Document) node).getDocumentElement();
if (!(node instanceof Element))
    return "";
Element el = (Element) node;
path = path.replaceAll("\\s", "");
if (path.startsWith("/"))
    path = path.substring(1);
...
StringgetTextContent(org.w3c.dom.Node element)
return the text content of an element
StringBuffer childtext = new StringBuffer();
NodeList childlist = element.getChildNodes();
int ct = childlist.getLength();
for (int j = 0; j < ct; j++) {
    org.w3c.dom.Node childNode = childlist.item(j);
    if ((childNode.getNodeType() == Node.TEXT_NODE)
            || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
        childtext.append(childNode.getNodeValue().trim());
...
StringgetTextContentOld(final Node node)
get Text Content Old
final Node child = node.getFirstChild();
if (child != null) {
    final Node next = child.getNextSibling();
    if (next == null) {
        return hasTextContent(child) ? child.getNodeValue() : "";
    final StringBuilder buf = new StringBuilder();
    appendTextContents(node, buf);
...
StringgetTextContents(Node node)
(A useful utility method from IBM developerworks)
NodeList childNodes;
StringBuffer contents = new StringBuffer();
childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
        contents.append(childNodes.item(i).getNodeValue());
return contents.toString();
StringgetTextData(Node element)
Gets a TEXT node value from the specified Element node.
if (element.getNodeType() != Node.ELEMENT_NODE) {
    throw new IllegalArgumentException(element + " is not ELEMENT node");
Node description = element.getNextSibling();
if (description.getNodeType() != Node.TEXT_NODE) {
    throw new IllegalArgumentException(description + " is not TEXT node");
return description.getNodeValue();
...
StringgetTextData(Node node)
get Text Data
if (!node.hasChildNodes()) {
    return null;
Node child = node.getFirstChild();
while (child != null && child.getNodeType() != Node.TEXT_NODE
        && child.getNodeType() != Node.CDATA_SECTION_NODE) {
    child = child.getNextSibling();
if (child == null) {
    return null;
if (child.getNodeType() == Node.TEXT_NODE) {
    return ((Text) child).getData();
} else {
    return ((CDATASection) child).getData();
StringgetTextForNode(Node node)
get Text For Node
StringBuilder sb = new StringBuilder();
NodeList children = node.getChildNodes();
if (children.getLength() == 0)
    return null;
for (int i = 0; i < children.getLength(); ++i) {
    Node n = children.item(i);
    if (n instanceof Text)
        sb.append(n.getNodeValue());
...
StringgetTextForNode(Node node)
get Text For Node
StringBuffer sb = new StringBuffer();
NodeList children = node.getChildNodes();
if (children.getLength() == 0)
    return null;
for (int i = 0; i < children.getLength(); ++i) {
    Node n = children.item(i);
    if (n instanceof Text)
        sb.append(n.getNodeValue());
...
StringgetTextForNode(Node node)
Gets the String value of the node.
NodeList children = node.getChildNodes();
if (children == null) {
    return "";
for (int i = 0; i < children.getLength(); i++) {
    Node childNode = children.item(i);
    if ((childNode.getNodeType() == Node.TEXT_NODE)
            || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
...
StringgetTextFromNode(Node node, String defaultValue)
get Text From Node
if (node == null) {
    return defaultValue;
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
    return ((Attr) node).getValue();
} else if (node.getNodeType() == Node.TEXT_NODE) {
    return node.getNodeValue();
} else {
...