Thursday, March 8, 2012

Dispalying the XML node as formatted string

Displaying the XML node as formatted string:

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Node;

public class TransformUtil {

public static String nodeToString(Node node) throws Exception{
StringWriter sw = new StringWriter();
try {
Transformer t = new org.apache.xalan.processor.TransformerFactoryImpl().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
throw te;
return sw.toString();


No comments:

Post a Comment