Class JAXB
- java.lang.Object
-
- jakarta.xml.bind.JAXB
-
public final class JAXB extends java.lang.ObjectClass that defines convenience methods for common, simple use of Jakarta XML Binding.Methods defined in this class are convenience methods that combine several basic operations in the
JAXBContext,Unmarshaller, andMarshaller. They are designed to be the prefered methods for developers new to Jakarta XML Binding. 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 Jakarta XML Binding API directly.
- Errors that happen during the processing is wrapped into
DataBindingException(which will haveJAXBExceptionas itscause. It is expected that people who prefer the checked exception would use the rest of the Jakarta XML Binding API directly.
In addition, the
unmarshalmethods 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
marshalmethods 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
unmarshalmethods either fail with an exception or return a non-null value.- Since:
- 1.6, JAXB 2.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classJAXB.CacheTo improve the performance, we'll cache the lastJAXBContextused.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ref.WeakReference<JAXB.Cache>cacheCache.
-
Constructor Summary
Constructors Modifier Constructor Description privateJAXB()No instantiation is allowed.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void_marshal(java.lang.Object jaxbObject, java.lang.Object xml)Writes a Java object tree to XML and store it to the specified location.private static <T> JAXBContextgetContext(java.lang.Class<T> type)Obtains theJAXBContextfrom the given type, by using the cache if possible.private static java.lang.StringinferName(java.lang.Class<?> clazz)static voidmarshal(java.lang.Object jaxbObject, java.io.File xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, java.io.OutputStream xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, java.io.Writer xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, java.lang.String xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, java.net.URI xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, java.net.URL xml)Writes a Java object tree to XML and store it to the specified location.static voidmarshal(java.lang.Object jaxbObject, javax.xml.transform.Result xml)Writes a Java object tree to XML and store it to the specified location.private static javax.xml.transform.ResulttoResult(java.lang.Object xml)CreatesResultfrom various XML representation.private static javax.xml.transform.SourcetoSource(java.lang.Object xml)CreatesSourcefrom various XML representation.static <T> Tunmarshal(java.io.File xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(java.io.InputStream xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(java.io.Reader xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(java.lang.String xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(java.net.URI xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(java.net.URL xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.static <T> Tunmarshal(javax.xml.transform.Source xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.
-
-
-
Field Detail
-
cache
private static volatile java.lang.ref.WeakReference<JAXB.Cache> cache
Cache. We don't want to prevent theJAXB.Cache.typefrom GC-ed, henceWeakReference.
-
-
Method Detail
-
getContext
private static <T> JAXBContext getContext(java.lang.Class<T> type) throws JAXBException
Obtains theJAXBContextfrom 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.Cacheandvolatile.- Throws:
JAXBException
-
unmarshal
public static <T> T unmarshal(java.io.File xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.- Parameters:
xml- Reads the entire file as XML.
-
unmarshal
public static <T> T unmarshal(java.net.URL xml, java.lang.Class<T> type)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
public static <T> T unmarshal(java.net.URI xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.- Parameters:
xml- The URI isturned into URLand then follows the handling ofURL.
-
unmarshal
public static <T> T unmarshal(java.lang.String xml, java.lang.Class<T> type)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
public static <T> T unmarshal(java.io.InputStream xml, java.lang.Class<T> type)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
public static <T> T unmarshal(java.io.Reader xml, java.lang.Class<T> type)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
public static <T> T unmarshal(javax.xml.transform.Source xml, java.lang.Class<T> type)Reads in a Java object tree from the given XML input.- Parameters:
xml- The XML infoset that theSourcerepresents is read.
-
toSource
private static javax.xml.transform.Source toSource(java.lang.Object xml) throws java.io.IOExceptionCreatesSourcefrom various XML representation. Seeunmarshal(java.io.File, java.lang.Class<T>)for the conversion rules.- Throws:
java.io.IOException
-
marshal
public static void marshal(java.lang.Object jaxbObject, java.io.File xml)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 hasXmlRootElementon 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 is inferred 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
public static void marshal(java.lang.Object jaxbObject, java.net.URL xml)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 hasXmlRootElementon 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 is inferred fromthe short class name. This parameter must not be null.xml- The XML will besentto the resource pointed by this URL. Note that not allURLs support such operation, and exact semantics depends on theURLimplementations. 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
public static void marshal(java.lang.Object jaxbObject, java.net.URI xml)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 hasXmlRootElementon 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 is inferred fromthe short class name. This parameter must not be null.xml- The URI isturned into URLand then follows the handling ofURL. See above.- Throws:
DataBindingException- If the operation fails, such as due to I/O error, unbindable classes.
-
marshal
public static void marshal(java.lang.Object jaxbObject, java.lang.String xml)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 hasXmlRootElementon 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 is inferred 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
public static void marshal(java.lang.Object jaxbObject, java.io.OutputStream xml)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 hasXmlRootElementon 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 is inferred 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
public static void marshal(java.lang.Object jaxbObject, java.io.Writer xml)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 hasXmlRootElementon 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 is inferred 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
public static void marshal(java.lang.Object jaxbObject, javax.xml.transform.Result xml)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 hasXmlRootElementon 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 is inferred fromthe short class name. This parameter must not be null.xml- The XML will be sent to theResultobject.- Throws:
DataBindingException- If the operation fails, such as due to I/O error, unbindable classes.
-
_marshal
private static void _marshal(java.lang.Object jaxbObject, java.lang.Object xml)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
JAXBContextandMarshaller. This method is designed to be the prefered method for developers new to Jakarta XML Binding. 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 Jakarta XML Binding API directly.
- Errors that happen during the processing is wrapped into
DataBindingException(which will haveJAXBExceptionas itscause. It is expected that those people who prefer the checked exception would use the rest of the Jakarta XML Binding 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 hasXmlRootElementon 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 is inferred fromthe short class name. This parameter must not be null.xml- Represents the receiver of XML. Objects of the following types are allowed.Allowed Objects Type Operation FileXML will be written to this file. If it already exists, it will be overwritten. URLThe XML will be sentto the resource pointed by this URL. Note that not allURLs support such operation, and exact semantics depends on theURLimplementations. In case ofHTTP URLs, this will perform HTTP POST.URIThe URI is turned into URLand then follows the handling ofURL. See above.StringThe string is first interpreted as an absolute URI. If it's nota valid absolute URI, then it's interpreted as aFileOutputStreamThe XML will be sent to the given OutputStream. Upon a successful completion, the stream will be closed by this method.WriterThe XML will be sent as a character stream to the given Writer. Upon a successful completion, the stream will be closed by this method.ResultThe XML will be sent to the Resultobject.- Throws:
DataBindingException- If the operation fails, such as due to I/O error, unbindable classes.
-
inferName
private static java.lang.String inferName(java.lang.Class<?> clazz)
-
toResult
private static javax.xml.transform.Result toResult(java.lang.Object xml) throws java.io.IOExceptionCreatesResultfrom various XML representation. See_marshal(Object,Object)for the conversion rules.- Throws:
java.io.IOException
-
-