public class XMLObjectWriter extends Object
This class takes an object and formats it to XML; the resulting
XML can be deserialized using a XMLObjectReader
.
When an object is formatted, the XMLFormat
of the
object's class as identified by the XMLBinding
is used to
write its XML representation.
Multiple objects can be written to the same XML output. For example:
XMLObjectWriter writer = XMLObjectWriter.newInstance(outputStream); while (true)) { Message message = ... writer.write(message, "Message", Message.class); } writer.close(); // The underlying stream is closed.
Constructor and Description |
---|
XMLObjectWriter()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Ends document writting, closes this writer and its underlying
output then
reset this Writer for potential reuse. |
void |
flush()
Flushes the output stream of this writer (automatically done
when
closing ). |
XMLStreamWriter |
getStreamWriter()
Returns the stream writer used by this object writer (it can be used
to write prolog, write namespaces, etc).
|
static XMLObjectWriter |
newInstance(OutputStream out)
Returns a XML object writer (potentially recycled) having the specified
output stream as output.
|
static XMLObjectWriter |
newInstance(OutputStream out,
String encoding)
Returns a XML object writer (potentially recycled) having the specified
output stream/encoding as output.
|
static XMLObjectWriter |
newInstance(Writer out)
Returns a XML object writer (potentially recycled) having the specified
writer as output.
|
void |
reset()
Resets this object writer for reuse.
|
XMLObjectWriter |
setBinding(XMLBinding binding)
Sets the XML binding to use with this object writer.
|
XMLObjectWriter |
setIndentation(String indentation)
Sets the indentation to be used by this writer (no indentation
by default).
|
XMLObjectWriter |
setOutput(OutputStream out)
Sets the output stream for this XML object writer.
|
XMLObjectWriter |
setOutput(OutputStream out,
String encoding)
Sets the output stream and encoding for this XML object writer.
|
XMLObjectWriter |
setOutput(Writer out)
Sets the output writer for this XML object writer.
|
XMLObjectWriter |
setReferenceResolver(XMLReferenceResolver referenceResolver)
Sets the XML reference resolver to use with this object writer
(the same reference resolver can be used accross multiple writers).
|
void |
write(Object obj)
Writes the specified object as an anonymous nested element of
unknown type.
|
void |
write(Object obj,
String name)
Writes the specified object as a named nested element of unknown type
(
null objects are ignored). |
void |
write(Object obj,
String localName,
String uri)
Writes the specified object as a fully qualified nested element of
unknown type (
null objects are ignored). |
<T> void |
write(T obj,
String name,
Class<T> cls)
Writes the specified object as a named nested element of actual type
known (
null objects are ignored). |
<T> void |
write(T obj,
String localName,
String uri,
Class<T> cls)
Writes the specified object as a fully qualified nested element of
actual type known (
null objects are ignored). |
public static XMLObjectWriter newInstance(OutputStream out) throws XMLStreamException
out
- the output stream.XMLStreamException
public static XMLObjectWriter newInstance(OutputStream out, String encoding) throws XMLStreamException
out
- the output stream.encoding
- the output stream encoding.XMLStreamException
public static XMLObjectWriter newInstance(Writer out) throws XMLStreamException
out
- the writer output.XMLStreamException
public XMLStreamWriter getStreamWriter()
public XMLObjectWriter setOutput(OutputStream out) throws XMLStreamException
out
- the output stream destination.this
XMLStreamException
XMLStreamWriterImpl.setOutput(OutputStream)
public XMLObjectWriter setOutput(OutputStream out, String encoding) throws XMLStreamException
out
- the output stream destination.encoding
- the stream encoding.this
XMLStreamException
XMLStreamWriterImpl.setOutput(OutputStream, String)
public XMLObjectWriter setOutput(Writer out) throws XMLStreamException
out
- the writer destination.this
XMLStreamException
XMLStreamWriterImpl.setOutput(Writer)
public XMLObjectWriter setBinding(XMLBinding binding)
binding
- the XML binding to use.this
public XMLObjectWriter setIndentation(String indentation)
indentation
- the indentation string.this
public XMLObjectWriter setReferenceResolver(XMLReferenceResolver referenceResolver)
referenceResolver
- the XML reference resolver.this
public void write(Object obj) throws XMLStreamException
obj
- the object written as nested element or null
.XMLStreamException
XMLFormat.OutputElement.add(Object)
public void write(Object obj, String name) throws XMLStreamException
null
objects are ignored). The nested XML element
may contain a class attribute identifying the object type.obj
- the object added as nested element or null
.name
- the name of the nested element.XMLStreamException
XMLFormat.OutputElement.add(Object, String)
public void write(Object obj, String localName, String uri) throws XMLStreamException
null
objects are ignored).
The nested XML element may contain a class attribute identifying
the object type.obj
- the object added as nested element or null
.localName
- the local name of the nested element.uri
- the namespace URI of the nested element.XMLStreamException
XMLFormat.OutputElement.add(Object, String, String)
public <T> void write(T obj, String name, Class<T> cls) throws XMLStreamException
null
objects are ignored).obj
- the object added as nested element or null
.name
- the name of the nested element.cls
- the non-abstract class identifying the XML format to use.XMLStreamException
XMLFormat.OutputElement.add(Object, String, Class)
public <T> void write(T obj, String localName, String uri, Class<T> cls) throws XMLStreamException
null
objects are ignored).obj
- the object added as nested element or null
.localName
- the local name of the nested element.uri
- the namespace URI of the nested element.cls
- the class identifying the XML format to use.XMLStreamException
XMLFormat.OutputElement.add(Object, String, String, Class)
public void flush() throws XMLStreamException
closing
).XMLStreamException
public void close() throws XMLStreamException
reset
this Writer for potential reuse.XMLStreamException
public void reset()
Copyright © 2005-2013 Javolution. All Rights Reserved.