Skip to content

Instantly share code, notes, and snippets.

@edalquist
Created March 28, 2012 16:26
Show Gist options
  • Save edalquist/2227971 to your computer and use it in GitHub Desktop.
Save edalquist/2227971 to your computer and use it in GitHub Desktop.
Eclipse XML Detail Formatter
When dealing with XML variables (Document, Node, Element) in the debug view of Eclipse
it can be handy to see the actual XML instead of the object model. Eclipse's Detail
Formatter feature makes this possible.
* Right click on the xml object in question
* Select "New Detail Formatter"
* Paste the code below into the Detail Formatter Code Snippet
* Enjoy!
if (this == null) return null;
javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = tf.newTransformer();
transformer.setOutputProperty( javax.xml.transform.OutputKeys.METHOD, "xml");
transformer.setOutputProperty("encoding", "UTF-8");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3" );
javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(this);
if (source == null) return "Corrupted XML document: " + this.toString();
java.io.StringWriter os = new java.io.StringWriter();
javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
transformer.transform(source,result);
return os.toString ();
@sbarsa
Copy link

sbarsa commented Sep 28, 2016

For dealing with a jdom Document, I've modified your Detail Formatter:

if (this == null) return null;

javax.xml.transform.TransformerFactory tf = javax.xml.transform.TransformerFactory.newInstance(); 
javax.xml.transform.Transformer transformer = tf.newTransformer();
transformer.setOutputProperty( javax.xml.transform.OutputKeys.METHOD, "xml");
transformer.setOutputProperty("encoding", "UTF-8");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3" );

org.w3c.dom.Document newDoc = new org.jdom.output.DOMOutputter().output(this);
javax.xml.transform.dom.DOMSource source = new javax.xml.transform.dom.DOMSource(newDoc);
if (source == null) return "Corrupted XML document: " + this.toString();

java.io.StringWriter os = new java.io.StringWriter();
javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(os);
transformer.transform(source,result);

return os.toString ();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment