Datasheet
29: ioe.printStackTrace();
30: }
31: Document d = p.getDocument();
32: OutputFormat format =
33: new OutputFormat(Method.XML,"UTF-8",true);
34: format.setPreserveSpace(true);
35: XMLSerializer serializer =
36: new XMLSerializer(System.out, format);
37: try {
38: serializer.serialize(d);
39: } catch (IOException ioe) {
40: ioe.printStackTrace();
41: }
42: }
43: }
Here you construct the OutputFormat and serializer and then pass the DOM Document object to the
serializer’s serialize method.
OutputFormat options
Now that you’ve seen examples of how to use the serializers, let’s look at OutputFormat in more detail.
A number of properties control how a serializer behaves. We’ll describe some of the more important
ones below in JavaBean style, so the property encoding has a getEncoding method and a setEncoding
method.
Property Description
String encoding The IANA name for the output character encoding.
String[] cDataElements An array of element names whose contents should be output
as CDATA.
int indent The number of spaces to indent.
boolean indenting True if the output should be indented.
String lineSeparator A string used to separate lines.
int lineWidth Lines longer than lineWidth characters are too long and are
wrapped/indented as needed.
String[] nonEscapingElements An array of element names whose contents should not be out-
put escaped (no character references are used).
boolean omitComments True if comments should not be output.
boolean omitDocumentType True if the DOCTYPE declaration should not be output.
boolean omitXMLDeclaration True if the XML declaration should not be output.
boolean preserveEmptyAttributes If false, then in HTML mode, empty attribute are output as
the attribute name only, with no equal sign or empty quotes.
boolean preserveSpace True if the serializer should preserve space that already exists
in the input.
37
Xerces
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 37