Datasheet
42: snse.printStackTrace();
43: }
44: try {
45: r.parse(args[0]);
46: } catch (IOException ioe) {
47: ioe.printStackTrace();
48: } catch (SAXException se) {
49: se.printStackTrace();
50: }
51: }
52: }
Note that you set up the serializer (in this case, an XMLSerializer) and then plug it into the XMLReader
as the callback handler for ContentHandler, DTDHandler, DeclHandler, and LexicalHandler.
A SAX version of the serializers might not seem interesting at first glance. Remember that SAX allows
you to build a pipeline-style conglomeration of XML processing components that implement the
org.xml.sax.XMLFilter interface. The SAX version of the serializers can be the last stage in one of these
pipelines. You can also write applications that accept the various SAX handlers as callbacks and that
then call the callbacks as a way of interfacing to other SAX components. Combining this approach with
the serializer classes is way to use SAX to generate XML from non-XML data, such as comma-delimited
or tab-delimited files.
The DOM version is a little more straightforward:
1: /*
2: *
3: * DOMSerializerMain.java
4: *
5: * Example from "Professional XML Development with Apache Tools"
6: *
7: */
8: package com.sauria.apachexml.ch1;
9:
10: import java.io.IOException;
11:
12: import org.apache.xerces.parsers.DOMParser;
13: import org.apache.xml.serialize.Method;
14: import org.apache.xml.serialize.OutputFormat;
15: import org.apache.xml.serialize.XMLSerializer;
16: import org.w3c.dom.Document;
17: import org.xml.sax.SAXException;
18:
19: public class DOMSerializerMain {
20:
21: public static void main(String[] args) {
22: DOMParser p = new DOMParser();
23:
24: try {
25: p.parse(args[0]);
26: } catch (SAXException se) {
27: se.printStackTrace();
28: } catch (IOException ioe) {
36
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 36