Is there a more elegant way to convert an XML Document to a String in Java than this code?

JavaXml

Java Problem Overview


Here is the code currently used.

public String getStringFromDoc(org.w3c.dom.Document doc) 	{
	    try
	    {
	       DOMSource domSource = new DOMSource(doc);
	       StringWriter writer = new StringWriter();
	       StreamResult result = new StreamResult(writer);
	       TransformerFactory tf = TransformerFactory.newInstance();
	       Transformer transformer = tf.newTransformer();
	       transformer.transform(domSource, result);
	       writer.flush();
	       return writer.toString();
	    }
	    catch(TransformerException ex)
	    {
	       ex.printStackTrace();
	       return null;
	    }
	}

Java Solutions


Solution 1 - Java

Relies on DOM Level3 Load/Save:

public String getStringFromDoc(org.w3c.dom.Document doc)    {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);   
}

Solution 2 - Java

The transformer API is the only XML-standard way to transform from a DOM object to a serialized form (String in this case). As standard I mean SUN Java XML API for XML Processing.

Other alternatives such as Xerces XMLSerializer or JDOM XMLOutputter are more direct methods (less code) but they are framework-specific.

In my opinion the way you have used is the most elegant and most portable of all. By using a standard XML Java API you can plug the XML-Parser or XML-Transformer of your choice without changing the code(the same as JDBC drivers). Is there anything more elegant than that?

Solution 3 - Java

This is a little more concise:

try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return result.getWriter().toString();
} catch(TransformerException ex) {
    ex.printStackTrace();
    return null;
}

Otherwise you could use a library like XMLSerializer from Apache:

//Serialize DOM
OutputFormat format    = new OutputFormat (doc); 
// as a String
StringWriter stringOut = new StringWriter ();    
XMLSerializer serial   = new XMLSerializer (stringOut,format);
serial.serialize(doc);
// Display the XML
System.out.println(stringOut.toString());

Solution 4 - Java

You could use XOM:

org.w3c.dom.Document domDocument = ...;
nu.xom.Document xomDocument = 
    nu.xom.converters.DOMConverter.convert(domDocument);
String xml = xomDocument.toXML();

You could use Jsoup:

org.jsoup.helper.W3CDom converter = new W3CDom();
String html = converter.asString( domDocument );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianView Question on Stackoverflow
Solution 1 - JavaykaganovichView Answer on Stackoverflow
Solution 2 - JavaFernando MiguélezView Answer on Stackoverflow
Solution 3 - JavadigitalsanctumView Answer on Stackoverflow
Solution 4 - JavatoolkitView Answer on Stackoverflow