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

StringgetText(Node node)
Return the text associated with a node, or null if no text section is present.
if (node == null)
    return null;
Node child = findChild(node, "#text");
if (child == null)
    return null;
return child.getNodeValue();
StringgetText(Node node)
Extract the textual content from a Node.
StringBuffer reply = new StringBuffer();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if ((child instanceof CharacterData && !(child instanceof Comment))
            || child instanceof EntityReference) {
        reply.append(child.getNodeValue());
    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
...
StringgetText(Node node)
get Text
if (node == null)
    return null;
if (node instanceof Text)
    return node.getNodeValue();
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
    if (children.item(i) instanceof Text)
        return children.item(i).getNodeValue();
...
StringgetTextBefore(Node node)
Get all the text DOM sibling nodes before the supplied node and concatenate them together into a single String.
Node parent = node.getParentNode();
if (parent == null) {
    System.out.println("Cannot get text before node [" + node + "]. [" + node + "] has no parent.");
    return "";
NodeList siblings = parent.getChildNodes();
StringBuffer text = new StringBuffer();
int siblingCount = siblings.getLength();
...
StringgetTextBetween(Node node1, Node node2)
Get all the text DOM sibling nodes before the supplied node and concatenate them together into a single String.
Node parent1 = node1.getParentNode();
if (parent1 == null) {
    System.out.println("Cannot get text between nodes [" + node1 + "] and [" + node2 + "]. [" + node1
            + "] has no parent.");
    return "";
Node parent2 = node2.getParentNode();
if (parent2 == null) {
...
voidgetTextBuffer(Node e, StringBuilder sb)
get Text Buffer
for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) {
    switch (kid.getNodeType()) {
    case Node.TEXT_NODE: {
        sb.append(kid.getNodeValue());
        break;
    case Node.ELEMENT_NODE: {
        getTextBuffer(kid, sb);
...
StringgetTextContent(final Node node)
get Text Content
boolean hasTextContent = false;
final StringBuffer buffer = new StringBuffer();
final NodeList nlist = node.getChildNodes();
for (int i = 0; i < nlist.getLength(); i++) {
    final Node child = nlist.item(i);
    if (child.getNodeType() == Node.TEXT_NODE) {
        buffer.append(child.getNodeValue());
        hasTextContent = true;
...
StringgetTextContent(final Node node)
get Text Content
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
case Node.ATTRIBUTE_NODE:
case Node.ENTITY_NODE:
case Node.ENTITY_REFERENCE_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
    return mergeTextContent(node.getChildNodes());
case Node.TEXT_NODE:
...
StringgetTextContent(final Node node, final String defaultValue)
Returns the text content of the given Node , null safe.
if (node == null) {
    return defaultValue;
return node.getTextContent();
voidgetTextContent(final Node node, final StringBuffer sb)
Internally used by getTextContent(Node node) method.
Node child = node.getFirstChild();
while (child != null) {
    if (child.getNodeType() == Node.TEXT_NODE) {
        sb.append(child.getNodeValue());
    } else {
        getTextContent(child, sb);
    child = child.getNextSibling();
...