Java Utililty Methods XML Element to String

List of utility methods to do XML Element to String

Description

The list of methods to do XML Element to String are organized into topic(s).

Method

StringextractText(Element obj, String localName)
extract Text
NodeList nodelist = obj.getElementsByTagNameNS("*", localName);
if (nodelist != null && nodelist.getLength() > 0) {
    Element node = (Element) nodelist.item(0);
    if (node != null) {
        String str = node.getTextContent();
        if (str != null) {
            str = str.trim();
            if (str.length() > 0)
...
StringextractTextElementValue(final Element textElement)
Trivial convenience method to extract the value of a (text) Element, coping with the possibility that text child Nodes may not have been coalesced.
NodeList childNodes = textElement.getChildNodes();
String result;
if (childNodes.getLength() == 1) {
    result = ensureExtractTextNodeValue(childNodes.item(0));
} else {
    StringBuilder resultBuilder = new StringBuilder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        resultBuilder.append(ensureExtractTextNodeValue(childNodes.item(i)));
...
InputStreamgetElementAsInputStream(Element xml)
get Element As Input Stream
Transformer transformer = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult res = new StreamResult(out);
transformer.transform(new DOMSource(xml), res);
return new ByteArrayInputStream(out.toByteArray());
StringgetElementText(Element e)
get Element Text
final StringBuilder val = new StringBuilder();
for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
    if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
        val.append(n.getNodeValue());
return val.toString();
StringgetNamedElemXml(Element parent, String elementName)
get Named Elem Xml
String val = null;
NodeList list = parent.getElementsByTagName(elementName);
if (list.getLength() > 0) {
    val = getXmlString(list.item(0));
return val;
StringgetString(Element el, String label)
get String
NodeList l = el.getElementsByTagName(label);
if (l.getLength() < 1)
    return null;
Node n = l.item(0).getFirstChild();
return n == null ? "" : n.getNodeValue();
StringgetString(Element element)
get String
StringBuffer buffer = new StringBuffer();
_getString(element, buffer);
return (new String(buffer));
StringgetString(Element element, String expr)
get String
try {
    XPath xPath = XPathFactory.newInstance().newXPath();
    return xPath.evaluate(expr + "/child::text()", element, XPathConstants.STRING).toString();
} catch (Exception e) {
    return "";
StringgetString(Element element, String name, String def)
Utility to get a String attribute from the given DOM Element.
if (!element.hasAttribute(name)) {
    return def;
return element.getAttribute(name);
StringgetString(Element root, String name)
get String
try {
    Element e = (Element) root.getElementsByTagName(name).item(0);
    return e.getTextContent();
} catch (Exception e) {
    System.err.println("Failed to parse String for tag " + name + "!");
return "NOT SET";