KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleTransform


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: SimpleTransform.java,v 1.11 2004/02/17 19:08:36 minchau Exp $
18  */

19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import javax.xml.transform.Transformer JavaDoc;
25 import javax.xml.transform.TransformerConfigurationException JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30
31 /**
32  * Use the TraX interface to perform a transformation in the simplest manner possible
33  * (3 statements).
34  */

35 public class SimpleTransform
36 {
37     public static void main(String JavaDoc[] args)
38     throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc,
39            FileNotFoundException JavaDoc, IOException JavaDoc
40   {
41   // Use the static TransformerFactory.newInstance() method to instantiate
42
// a TransformerFactory. The javax.xml.transform.TransformerFactory
43
// system property setting determines the actual class to instantiate --
44
// org.apache.xalan.transformer.TransformerImpl.
45
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
46     
47     // Use the TransformerFactory to instantiate a Transformer that will work with
48
// the stylesheet you specify. This method call also processes the stylesheet
49
// into a compiled Templates object.
50
Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc("birds.xsl"));
51
52     // Use the Transformer to apply the associated Templates object to an XML document
53
// (foo.xml) and write the output to a file (foo.out).
54
transformer.transform(new StreamSource JavaDoc("birds.xml"), new StreamResult JavaDoc(new FileOutputStream JavaDoc("birds.out")));
55     
56     System.out.println("************* The result is in birds.out *************");
57   }
58 }
59
Popular Tags