A Marshaller that serializes Java Object's to XML
Note: This class is not thread safe, and not intended to be,
so please create a new Marshaller for each thread if it
is to be used in a multithreaded environment.
addProcessingInstruction
public void addProcessingInstruction(String target,
String data)
Adds the given processing instruction data to the set of
processing instructions to output during marshalling.
target
- the processing instruction targetdata
- the processing instruction data
getMarshalExtendedType
public boolean getMarshalExtendedType()
If True the marshaller will use the 'xsi:type' attribute
to marshall a field value that extended the defined field type.
Default is True.
- If True the marshaller will use the 'xsi:type' attribute
to marshall a field value that extended the defined field type.
Default is True.
getNSPrefixAtRoot
public boolean getNSPrefixAtRoot()
Returns True if the given namespace mappings will be declared at the root node.
- Returns True if the given namespace mappings will be declared at the root node.
getResolver
public ClassDescriptorResolver getResolver()
Returns the ClassDescriptorResolver for use during marshalling
- the ClassDescriptorResolver
getRootElement
public String getRootElement()
Returns the name of the root element to use
- Returns the name of the root element to use
marshal
public void marshal(Object object)
throws MarshalException,
ValidationException
Marshals the given Object as XML using the DocumentHandler
for this Marshaller.
object
- The Object to marshal.
marshal
public static void marshal(Object object,
ContentHandler handler)
throws MarshalException,
ValidationException,
IOException
Marshals the given Object as XML using the given ContentHandler
to send events to.
object
- The Object to marshal.handler
- The ContentHandler to marshal to.
marshal
public static void marshal(Object object,
DocumentHandler handler)
throws MarshalException,
ValidationException
Marshals the given Object as XML using the given DocumentHandler
to send events to.
object
- The Object to marshal.handler
- The DocumentHandler to marshal to.
marshal
public static void marshal(Object object,
Node node)
throws MarshalException,
ValidationException
Marshals the given Object as XML using the given DOM Node
to send events to.
object
- The Object to marshal.node
- The DOM Node to marshal to.
marshal
public static void marshal(Object object,
Writer out)
throws MarshalException,
ValidationException
Marshals the given Object as XML using the given writer.
object
- The Object to marshal.out
- The writer to marshal to.
setDoctype
public void setDoctype(String publicId,
String systemId)
Sets the document type definition for the serializer. Note that this method
cannot be called if you've passed in your own DocumentHandler.
publicId
- the public identifiersystemId
- the system identifier
setEncoding
public void setEncoding(String encoding)
Sets the encoding for the serializer. Note that this method
cannot be called if you've passed in your own DocumentHandler.
encoding
- the encoding to set
setLogWriter
public void setLogWriter(PrintWriter printWriter)
Sets the PrintWriter used for logging
printWriter
- the PrintWriter to use for logging
setMapping
public void setMapping(Mapping mapping)
throws MappingException
Sets the given mapping to be used by the marshalling Framework. If a resolver
exists this mapping will be added to the existing ClassDescriptorResolver.
Otherwise a new ClassDescriptorResolver will be created.
mapping
- Mapping to using during marshalling.
setMarshalAsDocument
public void setMarshalAsDocument(boolean asDocument)
Sets whether or not to marshal as a document which includes
the XML declaration, and if necessary the DOCTYPE declaration.
By default the Marshaller will marshal as a well formed
XML document including the XML Declaration.
If the given boolean is false, the Marshaller will marshal
as a well formed XML fragment (no XML declaration or DOCTYPE).
This method is basically the same as calling
#setSupressXMLDeclaration(true);
asDocument
- a boolean, when true, indicating to marshal
as a complete XML document.
setMarshalExtendedType
public void setMarshalExtendedType(boolean marshalExtendedType)
If True the marshaller will use the 'xsi:type' attribute
to marshall a field value that extended the defined field type.
Default is True.
setMarshalListener
public void setMarshalListener(MarshalListener listener)
Sets an optional MarshalListener to recieve pre and post
marshal notification for each Object in the tree.
MarshalListener is only for complex objects that map
into elements, simpleTypes and types that map into
attributes do not cause any pre and post event notifications.
Current only one (1) listener is allowed. If you need
register multiple listeners, you will have to create
your own master listener that will forward the
event notifications and manage the multiple
listeners.
listener
- the MarshalListener to set.
setNSPrefixAtRoot
public void setNSPrefixAtRoot(boolean nsPrefixAtRoot)
Set to True to declare the given namespace mappings at the root node. Default is False.
setNamespaceMapping
public void setNamespaceMapping(String nsPrefix,
String nsURI)
Sets the mapping for the given Namespace prefix
nsPrefix
- the namespace prefixnsURI
- the namespace that the prefix resolves to
setNoNamespaceSchemaLocation
public void setNoNamespaceSchemaLocation(String schemaLocation)
Sets the value for the xsi:noNamespaceSchemaLocation attribute.
When set, this attribute will appear on the root element
of the marshalled document.
schemaLocation
- the URI location of the schema
to which the marshalled document is an instance of.
setResolver
public void setResolver(XMLClassDescriptorResolver cdr)
Sets the ClassDescriptorResolver to use during marshalling
Note: This method will nullify any Mapping
currently being used by this Marshaller
cdr
- the ClassDescriptorResolver to use
setRootElement
public void setRootElement(String rootElement)
Sets the name of the root element to use.
rootElement
- The name of the root element to use.
setSchemaLocation
public void setSchemaLocation(String schemaLocation)
Sets the value for the xsi:schemaLocation attribute.
When set, this attribute will appear on the root element
of the marshalled document.
schemaLocation
- the URI location of the schema
to which the marshalled document is an instance of.
setSuppressNamespaces
public void setSuppressNamespaces(boolean suppressNamespaces)
Sets whether or not namespaces are output. By default
the Marshaller will output namespace declarations and
prefix elements and attributes with their respective
namespace prefix. This method can be used to prevent
the usage of namespaces.
suppressNamespaces
- a boolean that when true
will prevent namespaces from being output.
setSuppressXSIType
public void setSuppressXSIType(boolean suppressXSIType)
Sets whether or not the xsi:type attribute should appear
on the marshalled document.
suppressXSIType
- a boolean that when true will prevent
xsi:type attribute from being used in the marshalling process.
setSupressXMLDeclaration
public void setSupressXMLDeclaration(boolean supressXMLDeclaration)
Sets whether or not to marshal as a document which includes
the XML declaration, and if necessary the DOCTYPE declaration.
By default the Marshaller will marshal as a well formed
XML document including the XML Declaration.
If the given boolean is true, the Marshaller will marshal
as a well formed XML fragment (no XML declaration or DOCTYPE).
This method is basically the same as calling
#setMarshalAsDocument(false);
supressXMLDeclaration
- a boolean that when true
includes that generated XML should not contain
the XML declaration.
setUseXSITypeAtRoot
public void setUseXSITypeAtRoot(boolean useXSITypeAtRoot)
Sets whether or not to output the xsi:type at the root
element. This is usually needed when the root element
type cannot be determined by the element name alone.
By default xsi:type will not be output on the root
element.
useXSITypeAtRoot
- a boolean that when true indicates
that the xsi:type should be output on the root element.
setValidation
public void setValidation(boolean validate)
Sets whether or not to validate the object model
before marshalling. By default validation is enabled.
This method is really for debugging.
I do not recommend turning off validation, since
you could marshal a document, which you can then
not unmarshal. If you know the object model
is guaranteed to be valid, disabling validation will
improve performace.
validate
- the boolean indicating whether or not to
validate the object model before marshalling.
setWriter
public void setWriter(Writer out)
throws IOException
Sets the java.io.Writer to be used during marshalling.
out
- The writer to use for marshalling