Methods defined in this class are convenience methods that combine several basic operations
in the JAXBContext
, Unmarshaller
, and Marshaller
.
They are designed
to be the prefered methods for developers new to JAXB. They have
the following characterstics:
- Generally speaking, the performance is not necessarily optimal. It is expected that people who need to write performance critical code will use the rest of the JAXB API directly.
- Errors that happen during the processing is wrapped into
DataBindingException
(which will haveJAXBException
as itscause
. It is expected that people who prefer the checked exception would use the rest of the JAXB API directly.
In addition, the unmarshal
methods have the following characteristic:
- Schema validation is not performed on the input XML.
The processing will try to continue even if there
are errors in the XML, as much as possible. Only as
the last resort, this method fails with
DataBindingException
.
Similarly, the marshal
methods have the following characteristic:
- The processing will try to continue even if the Java object tree
does not meet the validity requirement. Only as
the last resort, this method fails with
DataBindingException
.
All the methods on this class require non-null arguments to all parameters.
The unmarshal
methods either fail with an exception or return
a non-null value.
- Since:
- 1.6, JAXB 2.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
To improve the performance, we'll cache the lastJAXBContext
used. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
Writes a Java object tree to XML and store it to the specified location.private static <T> JAXBContext
getContext
(Class<T> type) Obtains theJAXBContext
from the given type, by using the cache if possible.private static String
static void
Writes a Java object tree to XML and store it to the specified location.static void
marshal
(Object jaxbObject, OutputStream xml) Writes a Java object tree to XML and store it to the specified location.static void
Writes a Java object tree to XML and store it to the specified location.static void
Writes a Java object tree to XML and store it to the specified location.static void
Writes a Java object tree to XML and store it to the specified location.static void
Writes a Java object tree to XML and store it to the specified location.static void
Writes a Java object tree to XML and store it to the specified location.private static Result
CreatesResult
from various XML representation.private static Source
CreatesSource
from various XML representation.static <T> T
Reads in a Java object tree from the given XML input.static <T> T
unmarshal
(InputStream xml, Class<T> type) Reads in a Java object tree from the given XML input.static <T> T
Reads in a Java object tree from the given XML input.static <T> T
Reads in a Java object tree from the given XML input.static <T> T
Reads in a Java object tree from the given XML input.static <T> T
Reads in a Java object tree from the given XML input.static <T> T
Reads in a Java object tree from the given XML input.
-
Field Details
-
cache
Cache. We don't want to prevent theJAXB.Cache.type
from GC-ed, henceWeakReference
.
-
-
Constructor Details
-
JAXB
private JAXB()No instanciation is allowed.
-
-
Method Details
-
getContext
Obtains theJAXBContext
from the given type, by using the cache if possible.We don't use locks to control access to
cache
, but this code should be thread-safe thanks to the immutableJAXB.Cache
andvolatile
.- Throws:
JAXBException
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- Reads the entire file as XML.
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The resource pointed by the URL is read in its entirety.
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The URI isturned into URL
and then follows the handling ofURL
.
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The string is first interpreted as an absoluteURI
. If it's nota valid absolute URI
, then it's interpreted as aFile
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The entire stream is read as an XML infoset. Upon a successful completion, the stream will be closed by this method.
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The character stream is read as an XML infoset. The encoding declaration in the XML will be ignored. Upon a successful completion, the stream will be closed by this method.
-
unmarshal
Reads in a Java object tree from the given XML input.- Parameters:
xml
- The XML infoset that theSource
represents is read.
-
toSource
CreatesSource
from various XML representation. Seeunmarshal(java.io.File, java.lang.Class<T>)
for the conversion rules.- Throws:
IOException
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- XML will be written to this file. If it already exists, it will be overwritten.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The XML will besent
to the resource pointed by this URL. Note that not allURL
s support such operation, and exact semantics depends on theURL
implementations. In case ofHTTP URLs
, this will perform HTTP POST.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The URI isturned into URL
and then follows the handling ofURL
. See above.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The string is first interpreted as an absoluteURI
. If it's nota valid absolute URI
, then it's interpreted as aFile
- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The XML will be sent to the givenOutputStream
. Upon a successful completion, the stream will be closed by this method.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The XML will be sent as a character stream to the givenWriter
. Upon a successful completion, the stream will be closed by this method.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
Writes a Java object tree to XML and store it to the specified location.- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- The XML will be sent to theResult
object.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
_marshal
Writes a Java object tree to XML and store it to the specified location.This method is a convenience method that combines several basic operations in the
JAXBContext
andMarshaller
. This method is designed to be the prefered method for developers new to JAXB. This method has the following characterstics:- Generally speaking, the performance is not necessarily optimal. It is expected that those people who need to write performance critical code will use the rest of the JAXB API directly.
- Errors that happen during the processing is wrapped into
DataBindingException
(which will haveJAXBException
as itscause
. It is expected that those people who prefer the checked exception would use the rest of the JAXB API directly.
- Parameters:
jaxbObject
- The Java object to be marshalled into XML. If this object is aJAXBElement
, it will provide the root tag name and the body. If this object hasXmlRootElement
on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name isinfered
fromthe short class name
. This parameter must not be null.xml
- Represents the receiver of XML. Objects of the following types are allowed.Type Operation File
XML will be written to this file. If it already exists, it will be overwritten. URL
The XML will be sent
to the resource pointed by this URL. Note that not allURL
s support such operation, and exact semantics depends on theURL
implementations. In case ofHTTP URLs
, this will perform HTTP POST.URI
The URI is turned into URL
and then follows the handling ofURL
. See above.String
The string is first interpreted as an absolute URI
. If it's nota valid absolute URI
, then it's interpreted as aFile
OutputStream
The XML will be sent to the given OutputStream
. Upon a successful completion, the stream will be closed by this method.Writer
The XML will be sent as a character stream to the given Writer
. Upon a successful completion, the stream will be closed by this method.Result
The XML will be sent to the Result
object.- Throws:
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
-
inferName
-
toResult
CreatesResult
from various XML representation. See_marshal(Object,Object)
for the conversion rules.- Throws:
IOException
-