1 9 10 package org.dom4j.samples.jaxp; 11 12 import org.dom4j.samples.SAXDemo; 13 14 import java.io.StringWriter ; 15 16 import javax.xml.transform.Result ; 17 import javax.xml.transform.Source ; 18 import javax.xml.transform.Transformer ; 19 import javax.xml.transform.TransformerFactory ; 20 21 import org.dom4j.Document; 22 import org.dom4j.io.DocumentSource; 23 import org.dom4j.io.OutputFormat; 24 import org.dom4j.io.XMLResult; 25 26 33 public class PrettyPrintDemo extends SAXDemo { 34 35 public static void main(String [] args) { 36 run(new PrettyPrintDemo(), args); 37 } 38 39 public PrettyPrintDemo() { 40 } 41 42 43 protected void process(Document document) throws Exception { 44 TransformerFactory factory = TransformerFactory.newInstance(); 46 Transformer transformer = factory.newTransformer(); 47 48 Source source = new DocumentSource(document); 50 51 OutputFormat format = OutputFormat.createPrettyPrint(); 53 StringWriter buffer = new StringWriter (); 54 Result result = new XMLResult(buffer, format); 55 56 transformer.transform(source, result); 58 59 String text = buffer.toString(); 60 System.out.println("The document is:- "); 61 System.out.println(text); 62 } 63 } 64 65 103 | Popular Tags |