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; com.sun.org.apache.xml.internal.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.
Method
, OutputPropertiesFactory
, SerializerFactory
, DOMSerializer
, ContentHandler
Modifier and Type | Method and Description |
---|---|
public ContentHandler | Returns: AContentHandler interface into this serializer,
or null if the serializer is not SAX 2 capableReturn a |
public Object | Returns: An Object to be cast to a DOM3Serializer interface into this serializer, or null if the serializer is not DOM capableReturn an Object into this serializer to be cast to a DOM3Serializer. |
public DOMSerializer | Returns: ADOMSerializer interface into this serializer,
or null if the serializer is not DOM capableReturn a |
public Properties | Returns: The output format key/value pairs in use.Returns the output format properties for this serializer. |
public OutputStream | Returns: reference to the result stream, or null if only a writer was set.Get the output stream where the events will be serialized to. |
public Writer | Returns: Reference to the result Writer, or null.Get the character stream where the events will be serialized to. |
public boolean | Returns: True if serializer has been reset and can be reusedThis method resets the serializer. |
public void | setOutputFormat(Properties
The output format to use, as a set of key/value pairs. format)Specifies an output format for this serializer. |
public void | setOutputStream(OutputStream
The output stream output)Specifies an output stream to which the document should be serialized. |
public void |
asContentHandler | back to summary |
---|---|
public ContentHandler asContentHandler() throws IOException Return a In principle only one of asDOMSerializer() or asContentHander() should be called.
|
asDOM3Serializer | back to summary |
---|---|
public Object asDOM3Serializer() throws IOException Return an Object into this serializer to be cast to a DOM3Serializer.
Through the returned object the document to be serialized,
a DOM (Level 3), can be provided to the serializer.
If the serializer does not support casting to a In principle only one of asDOM3Serializer() or asContentHander() should be called.
|
asDOMSerializer | back to summary |
---|---|
public DOMSerializer asDOMSerializer() throws IOException Return a In principle only one of asDOMSerializer() or asContentHander() should be called.
|
getOutputFormat | back to summary |
---|---|
public Properties getOutputFormat() Returns the output format properties for this serializer.
|
getOutputStream | back to summary |
---|---|
public OutputStream getOutputStream() Get the output stream where the events will be serialized to.
|
getWriter | back to summary |
---|---|
public Writer getWriter() Get the character stream where the events will be serialized to.
|
reset | back to summary |
---|---|
public boolean reset() This method resets the serializer. If this method returns true, the serializer may be used for subsequent serialization of new documents. It is possible to change the output format and output stream prior to serializing, or to reuse the existing output format and output stream or writer.
|
setOutputFormat | back to summary |
---|---|
public void setOutputFormat(Properties format) Specifies an output format for this serializer. It the serializer has already been associated with an output format, it will switch to the new format. This method should not be called while the serializer is in the process of serializing a document.
The standard property keys supported are: "method", "version", "encoding",
"omit-xml-declaration", "standalone", doctype-public",
"doctype-system", "cdata-section-elements", "indent", "media-type".
These property keys and their values are described in the XSLT recommendation,
see
The non-standard property keys supported are defined in This method can be called multiple times before a document is serialized. Each time it is called more, or over-riding property values, can be specified. One property value that can not be changed is that of the "method" property key. The value of the "cdata-section-elements" property key is a whitespace separated list of elements. If the element is in a namespace then value is passed in this format: {uri}localName If the "cdata-section-elements" key is specified on multiple calls to this method the set of elements specified in the value is not replaced from one call to the next, but it is cumulative across the calls.
|
setOutputStream | back to summary |
---|---|
public void setOutputStream(OutputStream output) Specifies an output stream to which the document should be serialized. This method should not be called while the serializer is in the process of serializing a document.
The encoding specified in the output Only one of setWriter() or setOutputStream() should be called.
|
setWriter | back to summary |
---|---|
public void setWriter(Writer writer) Specifies a writer to which the document should be serialized. This method should not be called while the serializer is in the process of serializing a document.
The encoding specified for the output Only one of setWriter() or setOutputStream() should be called.
|