1 2 17 20 21 25 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 29 import javax.xml.transform.Result ; 30 import javax.xml.transform.Templates ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.TransformerFactory ; 34 import javax.xml.transform.sax.SAXResult ; 35 import javax.xml.transform.sax.SAXSource ; 36 import javax.xml.transform.sax.SAXTransformerFactory ; 37 import javax.xml.transform.sax.TemplatesHandler ; 38 import javax.xml.transform.sax.TransformerHandler ; 39 40 import org.apache.xml.serializer.Serializer; 41 import org.apache.xml.serializer.SerializerFactory; 42 import org.apache.xml.serializer.OutputPropertiesFactory; 43 import org.xml.sax.SAXException ; 44 import org.xml.sax.XMLReader ; 45 import org.xml.sax.helpers.XMLReaderFactory ; 46 47 48 public class SAX2SAX 49 { 50 public static void main(String [] args) 51 throws TransformerException , TransformerConfigurationException , 52 SAXException , IOException 53 { 54 55 TransformerFactory tFactory = TransformerFactory.newInstance(); 57 if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) 60 { 61 SAXTransformerFactory saxTFactory = ((SAXTransformerFactory ) tFactory); 63 TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler(); 65 66 XMLReader reader = XMLReaderFactory.createXMLReader(); 68 reader.setContentHandler(templatesHandler); 69 70 reader.parse("birds.xsl"); 72 73 Templates templates = templatesHandler.getTemplates(); 75 TransformerHandler handler 77 = saxTFactory.newTransformerHandler(templates); 78 reader.setContentHandler(handler); 80 81 reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler); 84 85 FileOutputStream fos = new FileOutputStream ("birds.out"); 86 87 Serializer serializer = SerializerFactory.getSerializer 88 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 89 serializer.setOutputStream(fos); 90 91 92 Result result = new SAXResult (serializer.asContentHandler()); 94 handler.setResult(result); 95 96 reader.parse("birds.xml"); 98 99 System.out.println("************* The result is in birds.out *************"); 100 } 101 else 102 System.out.println("The TransformerFactory does not support SAX input and SAX output"); 103 } 104 } 105
| Popular Tags
|