|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
The Unmarshaller class governs the process of deserializing XML data into newly created Java content trees, optionally validating the XML data as it is unmarshalled. It provides the basic unmarshalling methods:
Unmarshalling from a file:
InputStream is = new InputStream( new FileInputStream( "nosferatu.xml" ) ); JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); Object o = u.unmarshal( is );
Unmarshalling from a URL:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); URL url = new URL( "http://beaker.east/nosferatu.xml" ); Object o = u.unmarshal( url );
Unmarshalling from a StringBuffer using a javax.xml.transform.stream.StreamSource:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); StringBuffer xmlStr = new StringBuffer( "<?xml version="1.0"?>..." ); Object o = u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) );
Unmarshalling from a javax.xml.transform.sax.SAXSource using a client specified validating SAX2.0 parser:
// configure a validating SAX2.0 parser (Xerces2) static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final String JAXP_SCHEMA_LOCATION = "http://java.sun.com/xml/jaxp/properties/schemaSource"; static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; System.setProperty( "javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl" ); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setValidating(true); SAXParser saxParser = spf.newSAXParser(); try { saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); saxParser.setProperty(JAXP_SCHEMA_LOCATION, "http://...."); } catch (SAXNotRecognizedException x) { // exception handling omitted } XMLReader xmlReader = saxParser.getXMLReader(); SAXSource source = new SAXSource( xmlReader, new InputSource( "http://..." ) ); // Setup JAXB to unmarshal JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); ValidationEventCollector vec = new ValidationEventCollector(); u.setEventHandler( vec ); // turn off the JAXB provider's default validation mechanism to // avoid duplicate validation u.setValidating( false ) // unmarshal Object o = u.unmarshal( source ); // check for events if( vec.hasEvents() ) { // iterate over events }
The JAXBContext object used to create this Unmarshaller
was initialized with a contextPath which determines the schema
derived content available to the Marshaller, Unmarshaller,
and Validator objects it produces. If the JAXBContext
object that was used to create this Unmarshaller does not have
enough information to know how to unmarshal the XML content from the
specified source, then the unmarshal operation will abort immediately by
throwing a UnmarshalException
.
Support for SAX2.0 Compliant Parsers
A client application has the ability to select the SAX2.0 compliant parser
of their choice. If a SAX parser is not selected, then the JAXB Provider's
default parser will be used. Eventhough the JAXB Provider's default parser
is not required to be SAX2.0 compliant, all providers are required to allow
a client application to specify their own SAX2.0 parser. Some providers may
require the client application to specify the SAX2.0 parser at schema compile
time. See unmarshal(Source)
for more detail.
Validation and Well-Formedness
A client application can enable or disable the provider's default validation mechanism via the setValidating API. Sophisticated clients can specify their own validating SAX 2.0 compliant parser and bypass the provider's default validation mechanism using the
unmarshal(Source)
API.For a more detailed definition of how validation errors and warnings are handled, see the
Validator
javadoc.
JAXBContext
,
Marshaller
,
Validator
Method Summary | |
ValidationEventHandler |
getEventHandler()
Deprecated. Return the current event handler or the default event handler if one hasn't been set. |
UnmarshallerHandler |
getUnmarshallerHandler()
Deprecated. Get an unmarshaller handler object that can be used as a component in an XML pipeline. |
boolean |
isValidating()
Deprecated. Indicates whether or not the Unmarshaller is configured to validate during unmarshal operations. |
void |
setEventHandler(ValidationEventHandler handler)
Deprecated. Allow an application to register a ValidationEventHandler. |
void |
setValidating(boolean validating)
Deprecated. Specifies whether or not the Unmarshaller should validate during unmarshal operations. |
java.lang.Object |
unmarshal(java.io.File f)
Deprecated. Unmarshal XML data from the specified file and return the resulting content tree. |
java.lang.Object |
unmarshal(org.xml.sax.InputSource source)
Deprecated. Unmarshal XML data from the specified SAX InputSource and return the resulting content tree. |
java.lang.Object |
unmarshal(java.io.InputStream is)
Deprecated. Unmarshal XML data from the specified InputStream and return the resulting content tree. |
java.lang.Object |
unmarshal(org.w3c.dom.Node node)
Deprecated. Unmarshal XML data from the specified DOM tree and return the resulting content tree. |
java.lang.Object |
unmarshal(javax.xml.transform.Source source)
Deprecated. Unmarshal XML data from the specified XML Source and return the resulting content tree. |
java.lang.Object |
unmarshal(java.net.URL url)
Deprecated. Unmarshal XML data from the specified URL and return the resulting content tree. |
Method Detail |
public java.lang.Object unmarshal(java.io.File f) throws JAXBException
f
- the file to unmarshal XML data from
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(java.io.InputStream is) throws JAXBException
is
- the InputStream to unmarshal XML data from
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(java.net.URL url) throws JAXBException
url
- the url to unmarshal XML data from
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(org.xml.sax.InputSource source) throws JAXBException
source
- the input source to unmarshal XML data from
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(org.w3c.dom.Node node) throws JAXBException
node
- the document/element to unmarshal XML data from.
The caller must support at least Document and Element.
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(javax.xml.transform.Source source) throws JAXBException
SAX 2.0 Parser Pluggability
A client application can choose not to use the default parser mechanism supplied with their JAXB provider. Any SAX 2.0 compliant parser can be substituted for the JAXB provider's default mechanism. To do so, the client application must properly configure a SAXSource containing an XMLReader implemented by the SAX 2.0 parser provider. If the SAXSource does not contain an XMLReader, then the JAXB provider's default parser mechanism will be used.
This parser replacement mechanism can also be used to replace the JAXB provider's unmarshal-time validation engine. The client application must properly configure their SAX 2.0 compliant parser to perform validation (as shown in the example above). Any SAXParserExceptions encountered by the parser during the unmarshal operation will be processed by the JAXB provider and converted into JAXB ValidationEvent objects which will be reported back to the client via the ValidationEventHandler registered with the Unmarshaller. Note: specifying a substitute validating SAX 2.0 parser for unmarshalling does not necessarily replace the validation engine used by the JAXB provider for performing on-demand validation.
The only way for a client application to specify an alternate parser mechanism to be used during unmarshal is via the unmarshal(SAXSource) API. All other forms of the unmarshal method (File, URL, Node, etc) will use the JAXB provider's default parser and validator mechanisms.
TODO: what if their XMLReader already has an ErrorHandler set? I'm currently clobbering it in the RI with our ErrorHandler that converts SAXParserExceptions into JAXB ValidationEvents.
TODO: include a matrix that shows which validator will be used based on the use of this api?
source
- the XML Source to unmarshal XML data from (providers are
only required to support SAXSource, DOMSource, and StreamSource)
JAXBException
- If any unexpected errors occur while unmarshalling
UnmarshalException
- If the Unmarshaller is unable to perform the XML to Java
binding. See Unmarshalling XML Datapublic UnmarshallerHandler getUnmarshallerHandler()
TODO: This method needs better documentation. It is not intended to be used by novice JAXB users - it is a more advanced feature available for clients wishing to integrate their JAXB unmarshal into an XML pipeline.
The JAXB Provider can return the same handler object for multiple invocations of this method. In other words, this method does not necessarily create a new instance of UnmarshallerHandler. If the application needs to use more than one UnmarshallerHandler, it should create more than one Unmarshaller.
UnmarshallerHandler
public void setValidating(boolean validating) throws JAXBException
This method may only be invoked before or after calling one of the unmarshal methods.
This method only controls the JAXB Provider's default unmarshal-time validation mechanism - it has no impact on clients that specify their own validating SAX 2.0 compliant parser. Clients that specify their own unmarshal-time validation mechanism may wish to turn off the JAXB Provider's default validation mechanism via this API to avoid "double validation".
validating
- true if the Unmarshaller should validate during
unmarshal, false otherwise
JAXBException
- if an error occurred while enabling or disabling
validation at unmarshal timepublic boolean isValidating() throws JAXBException
This API returns the state of the JAXB Provider's default unmarshal-time validation mechanism.
Note: I named this method isValidating() to stay in-line with JAXP, as opposed to naming it getValidating().
JAXBException
- if an error occurs while retrieving the validating
flagpublic void setEventHandler(ValidationEventHandler handler) throws JAXBException
The ValidationEventHandler will be called by the JAXB Provider if any validation errors are encountered during calls to any of the unmarshal methods. If the client application does not register a ValidationEventHandler before invoking the unmarshal methods, then ValidationEvents will be handled by the default event handler which will terminate the unmarshal operation after the first error or warning is encountered.
handler
- the validation event handler
JAXBException
- if an error was encountered while setting the
event handlerpublic ValidationEventHandler getEventHandler() throws JAXBException
JAXBException
- if an error was encountered while getting the
current event handler
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |