|
Oracle® Application Server XML Java API Reference 10g Release 3 (10.1.3) B28238-01 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
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:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); Object o = u.unmarshal( new File( "nosferatu.xml" ) );
Unmarshalling from an InputStream:
InputStream is = 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 org.w3c.dom.Node:
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" ); Unmarshaller u = jc.createUnmarshaller(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File( "nosferatu.xml")); Object o = u.unmarshal( doc );
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
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() Return the current event handler or the default event handler if one hasn't been set. |
UnmarshallerHandler |
getUnmarshallerHandler() Get an unmarshaller handler object that can be used as a component in an XML pipeline. |
boolean |
isValidating() Indicates whether or not the Unmarshaller is configured to validate during unmarshal operations. |
void |
setEventHandler(ValidationEventHandler handler) Allow an application to register a ValidationEventHandler. |
void |
setValidating(boolean validating) Specifies whether or not the default validation mechanism of the Unmarshaller should validate during unmarshal operations. |
java.lang.Object |
unmarshal(java.io.File f) Unmarshal XML data from the specified file and return the resulting content tree. |
java.lang.Object |
unmarshal(InputSource source) Unmarshal XML data from the specified SAX InputSource and return the resulting content tree. |
java.lang.Object |
unmarshal(java.io.InputStream is) Unmarshal XML data from the specified InputStream and return the resulting content tree. |
java.lang.Object |
unmarshal(Node node) Unmarshal XML data from the specified DOM tree and return the resulting content tree. |
java.lang.Object |
unmarshal(Source source) Unmarshal XML data from the specified XML Source and return the resulting content tree. |
java.lang.Object |
unmarshal(java.net.URL url) 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 fromJAXBException
- If any unexpected errors occur while unmarshallingUnmarshalException
- 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 fromJAXBException
- If any unexpected errors occur while unmarshallingUnmarshalException
- 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 fromJAXBException
- If any unexpected errors occur while unmarshallingUnmarshalException
- If the Unmarshaller is unable to perform the XML to Java binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(InputSource source) throws JAXBException
source
- the input source to unmarshal XML data fromJAXBException
- If any unexpected errors occur while unmarshallingUnmarshalException
- If the Unmarshaller is unable to perform the XML to Java binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(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 unmarshallingUnmarshalException
- If the Unmarshaller is unable to perform the XML to Java binding. See Unmarshalling XML Datapublic java.lang.Object unmarshal(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 XMLReader has an org.xml.sax.ErrorHandler registered on it, it will be replaced by the JAXB Provider so that validation errors can be reported via the ValidationEventHandler mechanism of JAXB. 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.
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 unmarshallingUnmarshalException
- If the Unmarshaller is unable to perform the XML to Java binding. See Unmarshalling XML Datapublic UnmarshallerHandler getUnmarshallerHandler()
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 otherwiseJAXBException
- 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.
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 fatal error is encountered.
handler
- the validation event handlerJAXBException
- 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
|
Oracle® Application Server XML Java API Reference 10g Release 3 (10.1.3) B28238-01 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |