KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DOM2DOM


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DOM2DOM.java,v 1.8 2004/02/17 19:06:27 minchau Exp $
18  */

19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.dom.DOMResult JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
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 JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39   /**
40    * Show how to transform a DOM tree into another DOM tree.
41    * This uses the javax.xml.parsers to parse both an XSL file
42    * and the XML file into a DOM, and create an output DOM.
43    */

44 public class DOM2DOM
45 {
46     public static void main(String JavaDoc[] args)
47     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc, FileNotFoundException JavaDoc,
48            ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc
49   {
50       TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
51
52     if(tFactory.getFeature(DOMSource.FEATURE) && tFactory.getFeature(DOMResult.FEATURE))
53     {
54       //Instantiate a DocumentBuilderFactory.
55
DocumentBuilderFactory JavaDoc dFactory = DocumentBuilderFactory.newInstance();
56
57       // And setNamespaceAware, which is required when parsing xsl files
58
dFactory.setNamespaceAware(true);
59       
60       //Use the DocumentBuilderFactory to create a DocumentBuilder.
61
DocumentBuilder JavaDoc dBuilder = dFactory.newDocumentBuilder();
62       
63       //Use the DocumentBuilder to parse the XSL stylesheet.
64
Document JavaDoc xslDoc = dBuilder.parse("birds.xsl");
65
66       // Use the DOM Document to define a DOMSource object.
67
DOMSource JavaDoc xslDomSource = new DOMSource JavaDoc(xslDoc);
68
69       // Set the systemId: note this is actually a URL, not a local filename
70
xslDomSource.setSystemId("birds.xsl");
71
72       // Process the stylesheet DOMSource and generate a Transformer.
73
Transformer JavaDoc transformer = tFactory.newTransformer(xslDomSource);
74
75       //Use the DocumentBuilder to parse the XML input.
76
Document JavaDoc xmlDoc = dBuilder.parse("birds.xml");
77       
78       // Use the DOM Document to define a DOMSource object.
79
DOMSource JavaDoc xmlDomSource = new DOMSource JavaDoc(xmlDoc);
80       
81       // Set the base URI for the DOMSource so any relative URIs it contains can
82
// be resolved.
83
xmlDomSource.setSystemId("birds.xml");
84       
85       // Create an empty DOMResult for the Result.
86
DOMResult JavaDoc domResult = new DOMResult JavaDoc();
87   
88       // Perform the transformation, placing the output in the DOMResult.
89
transformer.transform(xmlDomSource, domResult);
90       
91         //Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out
92
// using a default output format.
93
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 JavaDoc("DOM node processing not supported!");
101     }
102   }
103 }
104
Popular Tags