Thursday, June 26, 2008

Eclipse Detail Formatter for DOM

Using the Detail Formatter in Eclipse for XML DOM/Node Objects

Often java developers need the ability to examine DOM document/node during the Debug process in eclipse and the default toString() implementation of Document and Node are not sufficient enough. In such a case Detail Formatter in Eclipse IDE comes to rescue. To view the XML for a Document/Node set up a Detail Formatter in Eclipse preferences as shown below ...

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 ();


That should take care of it. For more detail explanation try the link below.
http://www.howardism.org/Technical/Eclipse/Eclipse_Detail_Formatter.html

0 comments: