Class JAXBSerialiser
This helper also translates the exceptions that can be thrown during a JAXB serialise/deserialise operation in a
JAXBRuntimeException-
Method Summary
Modifier and TypeMethodDescriptiondeserialise(File file) Deserialise a File of XML to an Object (or JAXBElement)Deserialise a stream of XML to an Object (or JAXBElement)<T> Tdeserialise(Class<T> clazz, String xml) Deserialise and cast to a particular type<T> Tdeserialise(Class<T> clazz, Element xml) Deserialise and cast to a particular type<T> Tdeserialise(Class<T> clazz, InputSource source) Deserialise an input and cast to a particular typedeserialise(String xml) Deserialise a String of XML to an Object (or JAXBElement)deserialise(XMLStreamReader reader) Deserialise an XMLStreamReader to an Object (or JAXBElement)deserialise(Node node) Deserialise a DOM Node to an Object (or JAXBElement)deserialise(InputSource source) javax.xml.bind.JAXBContextRetrieve the inner JAXBContext
NOTE: use of this method is strongly discouraged!static JAXBSerialisergetInstance(Class<?>... classes) Helper method to get a JAXBSerialiser for a set of classes with the underlying JAXB implementation picked using the default rules for JAXB acquisition
This is an expensive operation and so the result should ideally be cachedstatic JAXBSerialisergetInstance(String contextPath) Helper method to get a JAXBSerialiser from a JAXB Context Path (i.e.static JAXBSerialisergetInstance(javax.xml.bind.JAXBContext context) Helper method to get a JAXBSerialiser from an existing JAXBContext.
This is an expensive operation and so the result should ideally be cachedstatic JAXBSerialiserHelper method to get a JAXBSerialiser that uses EclipseLink MOXy for all operationsstatic JAXBSerialiserHelper method to get a JAXBSerialiser that uses EclipseLink MOXy for all operationsstatic voidHelper method to print a serialised object to stdout (for dev/debugging use)voidvoidserialise(Object obj, OutputStream os) voidvoidserialise(Object obj, XMLStreamWriter writer) Marshals the provided type to the given XMLStreamWriter, filtering the XML stream to remove duplicate xmlns="" events if present.voidHelper method to serialise an Object to an org.w3c.dom.DocumentvoidserialiseWithDirectWriter(Object obj, XMLStreamWriter writer) Marshals the provided type directly to the provided XMLStreamWriter.setEncoding(String encoding) Specify an output encoding to use when marshalling the XML data.setFragment(boolean fragment) Specify the value of jaxb.fragment used by the underlying marshallersetNoNamespaceSchemaLocation(String noNamespaceSchemaLocation) Specify an xsi:noNamespaceSchemaLocation in the generated XMLsetPrettyOutput(boolean pretty) Enable/Disable pretty printingOptionally specify the schema to use for all future serialisation/deserialisation methodssetSchemaLocation(String schemaLocation) Specify an xsi:schemaLocation attribute in the generated XML
-
Method Details
-
getContext
public javax.xml.bind.JAXBContext getContext()Retrieve the inner JAXBContext
NOTE: use of this method is strongly discouraged!- Returns:
-
setSchema
Optionally specify the schema to use for all future serialisation/deserialisation methodsNote that if this serializer is returned from and cached by a
JAXBSerialiserFactorythen use of this method could result in unintended side effects for other classes sharing the JAXBSerialiserFactory- Parameters:
schema-- Returns:
- this for method chaining
-
setPrettyOutput
Enable/Disable pretty printing- Parameters:
pretty-- Returns:
- this for method chaining
-
setEncoding
Specify an output encoding to use when marshalling the XML data. The default is UTF-8Note that if this serializer is returned from and cached by a
JAXBSerialiserFactorythen use of this method could result in unintended side effects for other classes sharing the JAXBSerialiserFactory- Parameters:
encoding-- Returns:
- this for method chaining
-
setSchemaLocation
Specify an xsi:schemaLocation attribute in the generated XMLNote that if this serializer is returned from and cached by a
JAXBSerialiserFactorythen use of this method could result in unintended side effects for other classes sharing the JAXBSerialiserFactory- Parameters:
schemaLocation-- Returns:
- this for method chaining
-
setNoNamespaceSchemaLocation
Specify an xsi:noNamespaceSchemaLocation in the generated XMLNote that if this serializer is returned from and cached by a
JAXBSerialiserFactorythen use of this method could result in unintended side effects for other classes sharing the JAXBSerialiserFactory- Parameters:
noNamespaceSchemaLocation-- Returns:
- this for method chaining
-
setFragment
Specify the value of jaxb.fragment used by the underlying marshallerNote that if this serializer is returned from and cached by a
JAXBSerialiserFactorythen use of this method could result in unintended side effects for other classes sharing the JAXBSerialiserFactory- Parameters:
fragment-- Returns:
- this for method chaining
- See Also:
-
Marshaller
-
deserialise
Deserialise a String of XML to an Object (or JAXBElement)- Parameters:
xml-- Returns:
-
deserialise
Deserialise a stream of XML to an Object (or JAXBElement)- Parameters:
is-- Returns:
-
deserialise
Deserialise an input and cast to a particular type- Parameters:
clazz-source-- Returns:
-
deserialise
Deserialise and cast to a particular type- Parameters:
clazz-xml- a String of XML- Returns:
-
deserialise
Deserialise and cast to a particular type- Parameters:
clazz-xml- an XML element- Returns:
-
deserialise
Deserialise a File of XML to an Object (or JAXBElement)- Parameters:
file-- Returns:
-
deserialise
-
deserialise
Deserialise an XMLStreamReader to an Object (or JAXBElement)- Parameters:
reader-- Returns:
-
deserialise
Deserialise a DOM Node to an Object (or JAXBElement)- Parameters:
node-- Returns:
-
serialiseToDocument
Helper method to serialise an Object to an org.w3c.dom.Document- Parameters:
obj- the object to serialise- Returns:
- a new Document containing the serialised form of the provided Object
-
serialise
-
serialise
-
serialise
-
serialise
-
serialiseWithDirectWriter
Marshals the provided type directly to the provided XMLStreamWriter. This is provided to allow callers to bypass the EclipseLink MOXy mitigation against XmlAnyElements which contain xmlns=""- Parameters:
obj-writer-
-
serialise
Marshals the provided type to the given XMLStreamWriter, filtering the XML stream to remove duplicate xmlns="" events if present. This is to work around an EclipseLink MOXy issue whereby XmlAnyElements which contain xmlns="" get that namespace definition duplicated via STaX- Parameters:
obj-writer-
-
print
Helper method to print a serialised object to stdout (for dev/debugging use)- Parameters:
obj-
-
serialise
-
getInstance
Helper method to get a JAXBSerialiser from an existing JAXBContext.
This is an expensive operation and so the result should ideally be cached- Parameters:
context- an existing JAXBContext- Returns:
-
getInstance
Helper method to get a JAXBSerialiser for a set of classes with the underlying JAXB implementation picked using the default rules for JAXB acquisition
This is an expensive operation and so the result should ideally be cached- Parameters:
classes-- Returns:
-
getInstance
Helper method to get a JAXBSerialiser from a JAXB Context Path (i.e. a package name or colon-delimited list of package names) with the underlying JAXB implementation picked using the default rules for JAXB acquisition
This is an expensive operation and so the result should ideally be cached- Parameters:
contextPath- a package name or colon-delimited list of package names- Returns:
-
getMoxy
Helper method to get a JAXBSerialiser that uses EclipseLink MOXy for all operations- Parameters:
contextPath-- Returns:
-
getMoxy
Helper method to get a JAXBSerialiser that uses EclipseLink MOXy for all operations- Parameters:
classes-- Returns:
-