/Users/lyon/j4p/src/xml/classInfo/XsltDomWriter.java
|
1 /**
2 * XslDomWriter.java
3 * @author Thomas Rowland
4 * @version 02-08-03
5 */
6
7 package xml.classInfo;
8
9 import futils.Futil;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.Node;
13 import org.xml.sax.SAXException;
14
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.transform.Transformer;
19 import javax.xml.transform.TransformerConfigurationException;
20 import javax.xml.transform.TransformerException;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.dom.DOMSource;
23 import javax.xml.transform.stream.StreamResult;
24 import java.io.File;
25 import java.io.IOException;
26
27 /*
28 * This program demonstrates how to parse an XML file and
29 * use XSLT to write it back out to a new XML file.
30 * Uses the DOM Level 2 api and JAXP 1.2.
31 */
32
33 public class XsltDomWriter {
34
35 public static void main(String argv [])
36 throws IOException {
37
38 File inFile = Futil.getReadFile("select an XML file");
39 File outFile = new File(inFile.getParent() + "\\NewClassInfo.xml");
40 Document document = parse("file:" + inFile.getAbsolutePath());
41 transform(document, outFile);
42 }
43
44
45 /*
46 * Parses an XML file, represented by a URI, and returns the DOM Document
47 */
48 private static Document parse(String uri)
49 throws IOException {
50
51 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
52 try {
53 //dbf.setValidating(true);
54 //dbf.setNamespaceAware(true);
55
56 DocumentBuilder db = dbf.newDocumentBuilder();
57 Document doc = db.parse(uri);
58 Element root = doc.getDocumentElement();
59 return doc;
60 } catch (ParserConfigurationException e) {
61 // Factory unable to create parser.
62 System.out.println(
63 "DocumentBuilderFactory cannot be instantiated.\n"
64 + e.getMessage());
65 } catch (SAXException e) {
66 // Parsing error.
67 System.out.println(
68 "** SAXException\n"
69 + e.getMessage());
70 //get the wrapped exception
71 Exception ex = e.getException();
72 if (ex != null)
73 ex.printStackTrace();
74 } catch (IOException e) {
75 e.printStackTrace();
76 }
77 return null;
78 }
79
80
81 /*
82 * Uses XSLT to write a DOM Document,
83 * which may have been modified, back out to an XML file.
84 */
85 private static void transform(Node node, File outFile) {
86 try {
87 // Obtain a Transformer
88 TransformerFactory tf = TransformerFactory.newInstance();
89 Transformer t = tf.newTransformer();
90
91 // Create a source object
92 DOMSource source = new DOMSource(node);
93
94 // Create a result object and perform the transform
95 StreamResult result = new StreamResult(outFile);
96 t.transform(source, result);
97
98 result = new StreamResult(System.out);
99 t.transform(source, result);
100 } catch (TransformerConfigurationException e) {
101 // Exception generated by the TransformerFactory
102 System.out.println(
103 "Error creating the Transformer"
104 + "\n" + e.getMessage());
105 Throwable x = e;
106 if (e.getException() != null)
107 x = e.getException();
108 x.printStackTrace();
109 } catch (TransformerException e) {
110 // Exception generated by the transformer
111 System.out.println(
112 "Error during transformation"
113 + "\n" + e.getMessage());
114 Throwable x = e;
115 if (e.getException() != null)
116 x = e.getException();
117 x.printStackTrace();
118 }
119 }
120
121 }//