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

StringgetDescendentText(Node node, String name)
get Descendent Text
Node d = getDescendent(node, name);
if (d != null) {
    return d.getTextContent().trim();
return null;
StringgetDirectText(org.w3c.dom.Element node)
get Direct Text
Node n = node.getFirstChild();
StringBuilder b = new StringBuilder();
while (n != null) {
    if (n.getNodeType() == Node.TEXT_NODE)
        b.append(n.getTextContent());
    n = n.getNextSibling();
return b.toString().trim();
...
StringgetElementText(Node elem)
Retrieves the text of a given element.
String value = null;
Node node = (elem != null) ? elem.getFirstChild() : null;
while (node != null) { 
    if (node.getNodeType() == Node.TEXT_NODE) { 
        if (value == null)
            value = node.getNodeValue();
        else
            value += node.getNodeValue();
...
NodegetFirstExtensionNode(Node extensionsNode)
Returns first extension node
final String extensionTag = "extension";
NodeList nodes = extensionsNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    Node child = nodes.item(i);
    if (extensionTag.equals(child.getNodeName()))
        return child;
return null;
...
NodegetFirstExtensionNodeFromWorkingSet(Node extensionsNode, String workingSetName)
Returns first node after the comment node with working set name
final String extensionTag = "extension";
final String commentTag = "#comment";
Node child;
NodeList nodes = extensionsNode.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
    child = nodes.item(i);
    if (commentTag.equals(child.getNodeName()) && workingSetName.equals(child.getNodeValue()))
        for (int j = i; j < nodes.getLength(); j++) {
...
StringgetFirstLevelTextContent(Node node)
get First Level Text Content
NodeList list = node.getChildNodes();
StringBuilder textContent = new StringBuilder();
for (int i = 0; i < list.getLength(); ++i) {
    Node child = list.item(i);
    if (child.getNodeType() == Node.TEXT_NODE)
        textContent.append(child.getTextContent());
return textContent.toString().trim();
...
StringgetFirstSubElementsInnerText(Node element, String subElementName)
get First Sub Elements Inner Text
for (Element n : getImmediateSubElements(element, subElementName)) {
    Node innerNode = n.getFirstChild();
    if (innerNode == null)
        continue;
    String innerText = innerNode.getNodeValue();
    if (innerText == null || innerText.length() < 1)
        continue;
    return innerText;
...
StringgetInnerXmlText(Node xmlNode)
get Inner Xml Text
StringBuilder result = new StringBuilder();
Document xmlDocument = xmlNode.getOwnerDocument();
DOMImplementation xmlDocumentImpl = xmlDocument.getImplementation();
DOMImplementationLS lsImpl = (DOMImplementationLS) xmlDocumentImpl.getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
NodeList childNodes = xmlNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    String childText = lsSerializer.writeToString(childNodes.item(i));
...
StringgetNamedItemText(NamedNodeMap map, String name)
Gets the actual text from a named item contained in the given node map.
Node namedItem = map.getNamedItem(name);
if (namedItem == null) {
    return null;
} else {
    return namedItem.getTextContent();
StringgetNamespaceURIFromPrefix(Node context, String prefix)
Given a prefix and a node, return the namespace URI that the prefix has been associated with.
short nodeType = context.getNodeType();
Node tempNode = null;
switch (nodeType) {
case Node.ATTRIBUTE_NODE: {
    tempNode = ((Attr) context).getOwnerElement();
    break;
case Node.ELEMENT_NODE: {
...