1 9 10 package org.dom4j.samples; 11 12 import javax.xml.transform.Source ; 13 import javax.xml.transform.Transformer ; 14 import javax.xml.transform.TransformerFactory ; 15 import javax.xml.transform.stream.StreamSource ; 16 17 import org.dom4j.Document; 18 import org.dom4j.io.DocumentResult; 19 import org.dom4j.io.DocumentSource; 20 21 27 public class XSLTDemo extends SAXDemo { 28 29 protected String xsl; 30 31 public static void main(String [] args) { 32 run(new XSLTDemo(), args); 33 } 34 35 public XSLTDemo() { 36 } 37 38 public void run(String [] args) throws Exception { 39 if (args.length < 2) { 40 printUsage(); 41 return; 42 } 43 44 int idx = format.parseOptions(args, 0); 45 if (args.length - idx < 2) { 46 printUsage(); 47 return; 48 } else { 49 writer = createXMLWriter(); 50 51 Document document = parse(args[idx++]); 52 53 xsl = args[idx++]; 54 55 process(document); 56 } 57 } 58 59 protected void printUsage() { 60 printUsage("<XML URL> <XSLT URL>"); 61 } 62 63 64 protected void process(Document document) throws Exception { 65 TransformerFactory factory = TransformerFactory.newInstance(); 67 Transformer transformer = factory.newTransformer(new StreamSource (xsl)); 68 69 Source source = new DocumentSource(document); 72 DocumentResult result = new DocumentResult(); 73 transformer.transform(source, result); 74 75 Document transformedDoc = result.getDocument(); 77 writer.write(transformedDoc); 78 } 79 80 } 81 82 120 | Popular Tags |