The Serializer interface is implemented by a serializer to enable users to:
- get and set streams or writers
- configure the serializer with key/value properties
- get an org.xml.sax.ContentHandler or a DOMSerializer to provide input to
Here is an example using the asContentHandler() method:
java.util.Properties props =
OutputPropertiesFactory.getDefaultMethodProperties(Method.TEXT);
Serializer ser = SerializerFactory.getSerializer(props);
java.io.PrintStream ostream = System.out;
ser.setOutputStream(ostream);
// Provide the SAX input events
ContentHandler handler = ser.asContentHandler();
handler.startDocument();
char[] chars = { 'a', 'b', 'c' };
handler.characters(chars, 0, chars.length);
handler.endDocument();
ser.reset(); // get ready to use the serializer for another document
// of the same output method (TEXT).
As an alternate to supplying a series of SAX events as input through the
ContentHandler interface, the input to serialize may be given as a DOM.
For example:
org.w3c.dom.Document inputDoc;
org.apache.xml.serializer.Serializer ser;
java.io.Writer owriter;
java.util.Properties props =
OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
Serializer ser = SerializerFactory.getSerializer(props);
owriter = ...; // create a writer to serialize the document to
ser.setWriter( owriter );
inputDoc = ...; // create the DOM document to be serialized
DOMSerializer dser = ser.asDOMSerializer(); // a DOM will be serialized
dser.serialize(inputDoc); // serialize the DOM, sending output to owriter
ser.reset(); // get ready to use the serializer for another document
// of the same output method.
This interface is a public API.
See Also: Method See Also: OutputPropertiesFactory See Also: SerializerFactory See Also: DOMSerializer See Also: ContentHandler |