August 20, 2009

Eclipse XML DetailFormatter

Having had to work with XML a lot for a current project I was tired of drilling down the DOM objects in Eclipse's debugger so I went looking on the net for an Eclipse debug detail formatter and found one here:

I do had to remove a few lines to get it to work so here is my version, enjoy formatted XML tags from a DOM tree:
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(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 ();

No comments:

Post a Comment