1 16 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.dom.DOMResult ; 31 import javax.xml.transform.dom.DOMSource ; 32 33 import org.apache.xml.serializer.Serializer; 34 import org.apache.xml.serializer.SerializerFactory; 35 import org.apache.xml.serializer.OutputPropertiesFactory; 36 import org.w3c.dom.Document ; 37 import org.xml.sax.SAXException ; 38 39 44 public class DOM2DOM 45 { 46 public static void main(String [] args) 47 throws TransformerException , TransformerConfigurationException , FileNotFoundException , 48 ParserConfigurationException , SAXException , IOException  49 { 50 TransformerFactory tFactory = TransformerFactory.newInstance(); 51 52 if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE)) 53 { 54 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 56 57 dFactory.setNamespaceAware(true); 59 60 DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); 62 63 Document xslDoc = dBuilder.parse("birds.xsl"); 65 66 DOMSource xslDomSource = new DOMSource (xslDoc); 68 69 xslDomSource.setSystemId("birds.xsl"); 71 72 Transformer transformer = tFactory.newTransformer(xslDomSource); 74 75 Document xmlDoc = dBuilder.parse("birds.xml"); 77 78 DOMSource xmlDomSource = new DOMSource (xmlDoc); 80 81 xmlDomSource.setSystemId("birds.xml"); 84 85 DOMResult domResult = new DOMResult (); 87 88 transformer.transform(xmlDomSource, domResult); 90 91 Serializer serializer = SerializerFactory.getSerializer 94 (OutputPropertiesFactory.getDefaultMethodProperties("xml")); 95 serializer.setOutputStream(System.out); 96 serializer.asDOMSerializer().serialize(domResult.getNode()); 97 } 98 else 99 { 100 throw new org.xml.sax.SAXNotSupportedException ("DOM node processing not supported!"); 101 } 102 } 103 } 104
| Popular Tags
|