public final class JAXB extends Object
Class that defines convenience methods for common, simple use of JAXB.
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:
DataBindingException
(which will have JAXBException
as its cause
. 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:
DataBindingException
. Similarly, the marshal
methods have the following characteristic:
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.
public static <T> T unmarshal(File xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- Reads the entire file as XML.public static <T> T unmarshal(URL xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- The resource pointed by the URL is read in its entirety.public static <T> T unmarshal(URI xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- The URI is turned into URL
and then follows the handling of URL
.public static <T> T unmarshal(String xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- The string is first interpreted as an absolute URI
. If it's not a valid absolute URI
, then it's interpreted as a File
public static <T> T unmarshal(InputStream xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- The entire stream is read as an XML infoset. Upon a successful completion, the stream will be closed by this method.public static <T> T unmarshal(Reader xml, Class<T> type)
Reads in a Java object tree from the given XML input.
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.public static <T> T unmarshal(Source xml, Class<T> type)
Reads in a Java object tree from the given XML input.
xml
- The XML infoset that the Source
represents is read.public static void marshal(Object jaxbObject, File xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the 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.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, URL xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- The XML will be sent
to the resource pointed by this URL. Note that not all URL
s support such operation, and exact semantics depends on the URL
implementations. In case of HTTP URLs
, this will perform HTTP POST.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, URI xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- The URI is turned into URL
and then follows the handling of URL
. See above.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, String xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- The string is first interpreted as an absolute URI
. If it's not a valid absolute URI
, then it's interpreted as a File
DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, OutputStream xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- The XML will be sent to the given OutputStream
. Upon a successful completion, the stream will be closed by this method.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, Writer xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- 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.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.public static void marshal(Object jaxbObject, Result xml)
Writes a Java object tree to XML and store it to the specified location.
jaxbObject
- The Java object to be marshalled into XML. If this object is a JAXBElement
, it will provide the root tag name and the body. If this object has XmlRootElement
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 is infered
from the short class name
. This parameter must not be null.xml
- The XML will be sent to the Result
object.DataBindingException
- If the operation fails, such as due to I/O error, unbindable classes.
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.