JavaTM Platform
Standard Ed. 6

javax.xml.bind
�ӿ� Unmarshaller

������֪ʵ���ࣺ
AbstractUnmarshallerImpl

public interface Unmarshaller

Unmarshaller ������� XML ���ݷ����л�Ϊ�´����� Java �������Ĺ��̣������ڽ���ʱ��ѡ�����֤ XML ���ݡ�����Ը��ֲ�ͬ�����������ṩ�������ص� unmarshal ������

�� File ���飺

JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal( new File( "nosferatu.xml" ) );
    

�� InputStream ���飺

InputStream is = new FileInputStream( "nosferatu.xml" );
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();
Object o = u.unmarshal( is );
    

�� 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 );
    

ʹ�� javax.xml.transform.stream.StreamSource �� StringBuffer ���飺

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() ) ) );
    

�� 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 );
    

ʹ�ÿͻ���ָ������֤ SAX2.0 �������� javax.xml.transform.sax.SAXSource ���飺

// 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
       }
    

�� StAX XMLStreamReader ���飺

JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();
 
javax.xml.stream.XMLStreamReader xmlStreamReader = 
javax.xml.stream.XMLInputFactory().newInstance().createXMLStreamReader( ... );
 
Object o = u.unmarshal( xmlStreamReader );
    

�� StAX XMLEventReader ���飺

JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();
 
javax.xml.stream.XMLEventReader xmlEventReader = 
javax.xml.stream.XMLInputFactory().newInstance().createXMLEventReader( ... );
 
Object o = u.unmarshal( xmlEventReader );
    

���� XML ����

������Է����л���ʾ���� XML �ĵ��� XML �ĵ������� XML ���ݡ�ͨ����ʹ������ȫ�������ĸ�Ԫ���������� unmarshal ���������㹻��ͨ������ȫ��Ԫ�����������Ͷ��嵽 JAXB ӳ����� JAXBContext ӳ�䣬��Щ unmarshal �������������� XML ���ݵĸ�Ԫ�صĽ��顣�� JAXBContext ӳ�䲻���Խ��� XML ���ݵĸ�Ԫ��ʱ��Ӧ�ó������ʹ������ declaredType ���н���ķ�������������̡��� XML ���ݵĸ�Ԫ�ض�Ӧ��ģʽ�еı���Ԫ������������£���Щ�������ڽ�������á�
unmarshal �����Ӳ����� null�������������޷��� XML ���ݵĸ����鵽 JAXB ӳ������򽫱���һ����������ͨ���׳� JAXBException ��ֹ�������̡�

����ȫ�������ĸ�Ԫ��

���� declaredType ������ unmarshal ����ʹ�� JAXBContext ������ XML ���ݵĸ�Ԫ�ء�JAXBContext ʵ�������������� Unmarshaller ��ʵ����JAXBContext ʵ��ά��ȫ�������� XML Ԫ�غ����Ͷ������Ƶ� JAXB ӳ�����ӳ�䡣�� unmarshal ������� JAXBContext �Ƿ���ڸ�Ԫ�ص� XML ���ƺ�/�� @xsi:type �� JAXB ӳ�����ӳ�䡣�����������ӳ�䣬��ʹ���ʵ��� JAXB ӳ���������� XML ���ݡ�ע�⣬����Ԫ������δ֪�Ҹ�Ԫ�ؾ��� @xsi:type ʱ����ʹ�ø� JAXB ӳ������Ϊ JAXBElement ֵ������ XML ���ݡ��� JAXBContext ����û�и�Ԫ�����Ƶ�ӳ�䣬Ҳû���� @xsi:type��������ڣ� ��ӳ��ʱ����ô��ͨ���׳� UnmarshalException ������ֹ���������ͨ��ʹ����һ�������Ľ��� declaredType �� unmarshal ���������Խ�������쳣����

�������������ͽ��н���

�� declaredType ������ unmarshal ����ʹӦ�ó���ʹ�ڸ�Ԫ�� XML ���Ƶ� JAXBContext �в�����ӳ��ʱ��Ҳ�ܹ������л� XML ���ݵĸ�Ԫ�ء��� unmarshaller ʹ��ָ��Ϊ declaredType �����ġ�Ӧ�ó����ṩ��ӳ���������Ԫ�ء�ע�⣬��ʹ��Ԫ�ص�Ԫ��������ͨ�� JAXBContext ӳ��ģ�declaredType ����Ҳ����д��ӳ�䣬�Ա���ʹ����Щ unmarshal ����ʱ�����л���Ԫ�ء����⣬�� XML ���ݵĸ�Ԫ�ؾ��� xsi:type ���ԣ��Ҹ����Ե�ֵ����һ��ͨ�� JAXBContext ӳ�䵽 JAXB ӳ��������Ͷ���ʱ����Ԫ�ص� xsi:type ���Խ������� unmarshal ������ declaredType ��������Щ�������Ƿ���һ�� JAXBElement<declaredType> ʵ�����±���ʾ��������÷��� JAXBElement ʵ�������ԡ�

ͨ�����������ͽ��н��������ص� JAXBElement
JAXBElement ���� ֵ
���� xml Ԫ������
ֵ declaredType ��ʵ��
declaredType unmarshal ������ declaredType ����
������ null ��ʵ��������δ֪��

������ͨ�� declaredType ���н���ķ�����һ��ʾ����

���� declaredType �� org.w3c.dom.Node ���飺

Schema fragment for example
<xs:schema>
<xs:complexType name="FooType">...<\xs:complexType>
<!-- global element declaration "PurchaseOrder" -->
<xs:element name="PurchaseOrder">
<xs:complexType>
<xs:sequence>
<!-- local element declaration "foo" -->
<xs:element name="foo" type="FooType"/>
                    ...
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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"));
Element  fooSubtree = ...; // traverse DOM till reach xml element foo, constrained by a 
// local element declaration in schema.
 
// FooType is the JAXB mapping of the type of local element declaration foo.
JAXBElement<FooType> foo = u.unmarshal( fooSubtree, FooType.class);
    

֧�ּ��� SAX2.0 �Ľ�����

�ͻ���Ӧ�ó����ܹ�ѡ���Լ��ļ��� SAX2.0 �Ľ����������û��ѡ�� SAX ����������ʹ�� JAXB �ṩ�ߵ�Ĭ�Ͻ����������� JAXB �ṩ�ߵ�Ĭ�Ͻ������������� SAX2.0 ���ݵģ��������ṩ�߱��������ͻ���Ӧ�ó���ָ�������Լ��� SAX2.0 ��������һЩ�ṩ�߿���Ҫ��ͻ���Ӧ�ó�����ģʽ����ʱָ�� SAX2.0 ���������й���ϸ��Ϣ������� unmarshal(Source)��

��֤�͸�ʽ������

�ͻ���Ӧ�ó������ͨ�� setSchema(javax.xml.validation.Schema) API �����û���� JAXP 1.3 ��֤���ơ����ӵĿͻ��˿���ָ�������Լ��ļ��� SAX 2.0 �Ľ���������ʹ�� unmarshal(Source) API ���ƿ� JAXP 1.3 ��֤���ơ�

����Ҫ�������Ч XML �������� JAXB 2.0 �ж���ģ���ʹ Unmarshaller Ĭ�ϵ���֤�¼������������ JAXB 1.0 �и�Ϊ���ɡ����� JAXBContext ע���� JAXB 1.0 �󶨽����������ɵ�ģʽ��������ʱ��Ĭ�ϵĽ�����֤���������� DefaultValidationEventHandler�������������������󣨻����֮����ֹ������������� JAXB 2.0 �ͻ���Ӧ�ó��򣬲�������ʽ�����Ĭ����֤��������Ĭ���¼�����ֻ��������������ʱ��ֹ���������

֧�ֵ�����

��ǰû���κ���Ҫ Unmarshaller �ϵ����� JAXB �ṩ�߶�֧�ֵ����ԡ����ǣ�һЩ�ṩ�߿���֧�������Լ����ض����ṩ�ߵ����Լ���

�����¼��ص�

Unmarshaller �ṩ�����ַ��Ļص����ƣ���Щ���������ڽ�����̵Ĺؼ����Ͻ����ض���Ӧ�ó���Ĵ������ڡ��ඨ��ġ��¼��ص��У������ڼ�ᴥ�������� JAXB ӳ�����е��ض���Ӧ�ó���Ĵ��롣���ⲿ��������������һ���ص��������д��������¼���������ͨ�����������¼��ص�������

���ඨ�塱���¼��ص����������κ� JAXB ӳ����ͨ������������·���ǩ���ķ���ָ���Լ����ض��ص�������

// This method is called immediately after the object is created and before the unmarshalling of this 
// object begins.The callback provides an opportunity to initialize JavaBean properties prior to unmarshalling.
   void beforeUnmarshal(Unmarshaller, Object parent);
 
//This method is called after all the properties (except IDREF) are unmarshalled for this object, 
//but before this object is set to the parent object.
void afterUnmarshal(Unmarshaller, Object parent);
 
�ඨ��Ļص�����Ӧ���ڻص�������Ҫ������ķǹ���������/���ֶ�ʱʹ�á�

�ⲿ�������ص��������� Unmarshaller.Listener ʵ��ʹ�� setListener(Listener) ע�ᡣ�ⲿ�������������лص��¼����Ӷ������ñ������ض���ص�������Ϊ���еķ�ʽ�����¼����ⲿ�������ڽ�����̽����鵽 JAXB Ԫ�ػ� JAXB ӳ����ʱ�����¼���

���ඨ��ġ��¼��ص��������ⲿ�������¼��ص������໥���������Զ�һ���¼�ͬʱ���á������������ص�����������ʱ������ Unmarshaller.Listener.beforeUnmarshal(Object, Object) �� Unmarshaller.Listener.afterUnmarshal(Object, Object) �ж����˳������ǽ��е��á�

�׳��쳣���¼��ص���������ֹ��ǰ�Ľ�����̡�

�����°汾��ʼ��
JAXB 1.0
������
JAXBContext, Marshaller, Validator

Ƕ����ժҪ
static class Unmarshaller.Listener
          

�� Unmarshaller ע������һ��ʵ�ֵ�ʵ�����Ա��ⲿ���������¼���

 
����ժҪ
<A extends XmlAdapter>
A
getAdapter(Class<A> type)
          ��ȡ��ָ�����͹�������������
 AttachmentUnmarshaller getAttachmentUnmarshaller()
           
 ValidationEventHandler getEventHandler()
          ���ص�ǰ���¼������������û�������¼����������򷵻�Ĭ�ϵ��¼���������
 Unmarshaller.Listener getListener()
          ������� Unmarshaller ע��� Unmarshaller.Listener��
 Object getProperty(String name)
          ��ȡ Unmarshaller �ײ�ʵ���е��ض����ԡ�
 Schema getSchema()
          ��ȡ����ִ�н���ʱ��֤�� JAXP 1.3 Schema ����
 UnmarshallerHandler getUnmarshallerHandler()
          ��ȡ������ XML �����е������ unmarshaller �����������
 boolean isValidating()
          �ѹ�ʱ�� �� JAXB 2.0 ��ʼ������� getSchema()
<A extends XmlAdapter>
void
setAdapter(Class<A> type, A adapter)
          �������õ� XmlAdapter ʵ����� unmarshaller ������
 void setAdapter(XmlAdapter adapter)
          �������õ� XmlAdapter ʵ����� unmarshaller ������
 void setAttachmentUnmarshaller(AttachmentUnmarshaller au)
          ������ cid������ id URI��������������Ϊ�������ݵĶ��������ݹ�����
 void setEventHandler(ValidationEventHandler handler)
          ����Ӧ�ó���ע�� ValidationEventHandler��
 void setListener(Unmarshaller.Listener listener)
           ��� Unmarshaller ע������¼��ص� Unmarshaller.Listener��
 void setProperty(String name, Object value)
          ���� Unmarshaller �ײ�ʵ���е��ض����ԡ�
 void setSchema(Schema schema)
          ָ��Ӧ������֤��������������ݵ� JAXP 1.3 Schema ����
 void setValidating(boolean validating)
          �ѹ�ʱ�� �� JAXB2.0 ��ʼ������� setSchema(javax.xml.validation.Schema)
 Object unmarshal(File f)
          ��ָ�����ļ����� XML ���ݲ����صõ�����������
 Object unmarshal(InputSource source)
          ��ָ���� SAX InputSource ���� XML ���ݲ����صõ�����������
 Object unmarshal(InputStream is)
          ��ָ���� InputStream ���� XML ���ݲ����صõ�����������
 Object unmarshal(Node node)
          ��ָ���� DOM ������ȫ�� XML ���ݲ����صõ�����������
<T> JAXBElement<T>
unmarshal(Node node, Class<T> declaredType)
          ͨ�� JAXB ӳ��� declaredType ���� XML ���ݲ����صõ�����������
 Object unmarshal(Reader reader)
          ��ָ���� Reader ���� XML ���ݲ����صõ�����������
 Object unmarshal(Source source)
          ��ָ���� XML Source ���� XML ���ݲ����صõ�����������
<T> JAXBElement<T>
unmarshal(Source source, Class<T> declaredType)
          �� declaredType ָ���� XML Source ���� XML ���ݲ����صõ�����������
 Object unmarshal(URL url)
          ��ָ���� URL ���� XML ���ݲ����صõ�����������
 Object unmarshal(XMLEventReader reader)
          ��ָ���� pull ���������� XML ���ݲ����صõ�����������
<T> JAXBElement<T>
unmarshal(XMLEventReader reader, Class<T> declaredType)
          ����Ԫ�ؽ��鵽 JAXB ӳ��� declaredType �����صõ�����������
 Object unmarshal(XMLStreamReader reader)
          ��ָ���� pull ���������� XML ���ݲ����صõ�����������
<T> JAXBElement<T>
unmarshal(XMLStreamReader reader, Class<T> declaredType)
          ����Ԫ�ؽ��鵽 JAXB ӳ��� declaredType �����صõ�����������
 

������ϸ��Ϣ

unmarshal

Object unmarshal(File f)
                 throws JAXBException
��ָ�����ļ����� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

������
f - �����н��� XML ���ݵ��ļ�
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� File ����Ϊ null

unmarshal

Object unmarshal(InputStream is)
                 throws JAXBException
��ָ���� InputStream ���� XML ���ݲ����صõ�����������ʹ��������ʽ�� unmarshal API ʱ����֤�¼�λ����Ϣ���ܲ�������

ʵ������ȫ�ָ�Ԫ����

������
is - �����н��� XML ���ݵ� InputStream
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� InputStream ����Ϊ null

unmarshal

Object unmarshal(Reader reader)
                 throws JAXBException
��ָ���� Reader ���� XML ���ݲ����صõ�����������ʹ��������ʽ�� unmarshal API ʱ����֤�¼�λ����Ϣ���ܲ�������������Ϊ Reader ���ṩϵͳ ID��

ʵ������ȫ�ָ�Ԫ����

������
reader - �����н��� XML ���ݵ� Reader
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� InputStream ����Ϊ null
�����°汾��ʼ��
JAXB 2.0

unmarshal

Object unmarshal(URL url)
                 throws JAXBException
��ָ���� URL ���� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

������
url - �����н��� XML ���ݵ� URL
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� URL ����Ϊ null

unmarshal

Object unmarshal(InputSource source)
                 throws JAXBException
��ָ���� SAX InputSource ���� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

������
source - �����н��� XML ���ݵ�����Դ
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� InputSource ����Ϊ null

unmarshal

Object unmarshal(Node node)
                 throws JAXBException
��ָ���� DOM ������ȫ�� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

������
node - Ҫ���н��� XML ���ݵ��ĵ�/Ԫ�ء����������ٱ���֧�� Document �� Element��
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� Node ����Ϊ nul
������
unmarshal(org.w3c.dom.Node, Class)

unmarshal

<T> JAXBElement<T> unmarshal(Node node,
                             Class<T> declaredType)
                         throws JAXBException
ͨ�� JAXB ӳ��� declaredType ���� XML ���ݲ����صõ�����������

ʵ���������������ͽ��н�����

������
node - ���н��� XML ���ݵ��ĵ�/Ԫ�ء����������ٱ���֧�� Document �� Element��
declaredType - �������� node �� XML ���ݵ��ʵ� JAXB ӳ���ࡣ
���أ�
node �� JAXB Element ��ʾ��ʽ
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ����κβ���Ϊ null
�����°汾��ʼ��
6.0

unmarshal

Object unmarshal(Source source)
                 throws JAXBException
��ָ���� XML Source ���� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

SAX 2.0 �������Ŀɲ�����

�ͻ���Ӧ�ó������ѡ��ʹ���� JAXB �ṩ��һ���ṩ��Ĭ�Ͻ��������ơ��κμ��� SAX 2.0 �Ľ�����������ȡ�� JAXB �ṩ�ߵ�Ĭ�ϻ��ơ�Ҫʵ����һ�㣬�ͻ���Ӧ�ó��������ȷ����һ������ XMLReader �� SAXSource�����Ҹ� XMLReader ���� SAX 2.0 �������ṩ��ʵ�ֵġ���� XMLReader ��һ������ע��� org.xml.sax.ErrorHandler����ʹ�� JAXB �ṩ���滻�����Ա�ͨ�� JAXB �� ValidationEventHandler ���Ʊ�����֤������� SAXSource ������ XMLReader����ʹ�� JAXB �ṩ�ߵ�Ĭ�Ͻ��������ơ�

Ҳ����ʹ�ô˽������滻�������滻 JAXB �ṩ�ߵĽ���ʱ��֤���档�ͻ���Ӧ�ó��������ȷ��������� SAX 2.0 �Ľ�������ִ����֤����������ʾ�����������ڽ�������ڼ��������κ� SAXParserExceptions ������ JAXB �ṩ�ߴ�����������ת��Ϊ JAXB ValidationEvent ������Щ����ͨ���Ѿ��� Unmarshaller ע��� ValidationEventHandler ������ͻ��ˡ�ע�� ��Ϊ��ʵ�ֽ����ָ��һ���滻��֤ SAX 2.0 ������ʱ�������滻 JAXB �ṩ��ʹ�õ���֤������ִ�а���Ӧ�����֤��

�ͻ���Ӧ�ó���ָ�����ڽ����ڼ�ʹ�õ��滻���������Ƶ�Ψһ������ͨ�� unmarshal(SAXSource) API������������ʽ�� unmarshal �������ļ���URL��Node �ȵȣ���ʹ�� JAXB �ṩ�ߵ�Ĭ�Ͻ���������֤�����ơ�

������
source - �����н��� XML ���ݵ� XML Source���ṩ��ֻ��֧�� SAXSource��DOMSource �� StreamSource��
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� Source ����Ϊ nul
������
unmarshal(javax.xml.transform.Source, Class)

unmarshal

<T> JAXBElement<T> unmarshal(Source source,
                             Class<T> declaredType)
                         throws JAXBException
�� declaredType ָ���� XML Source ���� XML ���ݲ����صõ�����������

ʵ���������������ͽ��н�����

����� SAX 2.0 �������Ŀɲ�����

������
source - �����н��� XML ���ݵ� XML Source���ṩ��ֻ��֧�� SAXSource��DOMSource �� StreamSource��
declaredType - �������� source �� xml ��Ԫ�ص��ʵ� JAXB ӳ����
���أ�
ͨ�� JAXB Element ȷ���� Java ����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ����κβ���Ϊ null
�����°汾��ʼ��
JAXB 2.0

unmarshal

Object unmarshal(XMLStreamReader reader)
                 throws JAXBException
��ָ���� pull ���������� XML ���ݲ����صõ�����������

ʵ������ȫ�ָ�Ԫ����

�˷����ٶ��ý��������� START_DOCUMENT �� START_ELEMENT �¼��ϡ����齫����ʼ�¼���ʼ������Ӧ����ֹ�¼�����������˷����ɹ����أ��� reader ��ָ����ֹ�¼�����ı�ǡ�

������
reader - Ҫ��ȡ�Ľ�������
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� reader ����Ϊ null
IllegalStateException - ��� reader û��ָ�� START_DOCUMENT �� START_ELEMENT �¼���
�����°汾��ʼ��
JAXB 2.0
������
unmarshal(javax.xml.stream.XMLStreamReader, Class)

unmarshal

<T> JAXBElement<T> unmarshal(XMLStreamReader reader,
                             Class<T> declaredType)
                         throws JAXBException
����Ԫ�ؽ��鵽 JAXB ӳ��� declaredType �����صõ�����������

�˷���ʵ������ declaredType ���н�����

�˷����ٶ��ý��������� START_DOCUMENT �� START_ELEMENT �¼��ϡ����齫����ʼ�¼���ʼ������Ӧ����ֹ�¼�����������˷����ɹ����أ��� reader ��ָ����ֹ�¼�����ı�ǡ�

������
reader - Ҫ��ȡ�Ľ�������
declaredType - �������� reader �� START_ELEMENT XML ���ݵ��ʵ� JAXB ӳ���ࡣ
���أ�
ͨ�� JAXB Element ��ʾ��ʽȷ����������
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ����κβ���Ϊ null
�����°汾��ʼ��
JAXB 2.0

unmarshal

Object unmarshal(XMLEventReader reader)
                 throws JAXBException
��ָ���� pull ���������� XML ���ݲ����صõ�����������

�˷�����һ������ȫ�ָ�������

�˷����ٶ��ý��������� START_DOCUMENT �� START_ELEMENT �¼��ϡ����齫����ʼ�¼���ʼ������Ӧ����ֹ�¼�����������˷����ɹ����أ��� reader ��ָ����ֹ�¼�����ı�ǡ�

������
reader - Ҫ��ȡ�Ľ�������
���أ�
�´����� java �������ĸ�����
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ��� reader ����Ϊ null
IllegalStateException - ��� reader û��ָ�� START_DOCUMENT �� START_ELEMENT �¼���
�����°汾��ʼ��
JAXB 2.0
������
unmarshal(javax.xml.stream.XMLEventReader, Class)

unmarshal

<T> JAXBElement<T> unmarshal(XMLEventReader reader,
                             Class<T> declaredType)
                         throws JAXBException
����Ԫ�ؽ��鵽 JAXB ӳ��� declaredType �����صõ�����������

�˷���ʵ������ declaredType ���н�����

�˷����ٶ��ý��������� START_DOCUMENT �� START_ELEMENT �¼��ϡ����齫����ʼ�¼���ʼ������Ӧ����ֹ�¼�����������˷����ɹ����أ��� reader ��ָ����ֹ�¼�����ı�ǡ�

������
reader - Ҫ��ȡ�Ľ�������
declaredType - �������� reader �� START_ELEMENT XML ���ݵ��ʵ� JAXB ӳ���ࡣ
���أ�
ͨ�� JAXB Element ��ʾ��ʽȷ����������
�׳���
JAXBException - ����ڽ���ʱ��������Ԥ�ϵĴ���
UnmarshalException - ��� ValidationEventHandler ���� handleEvent �������� false ���� Unmarshaller ����ִ�� XML �� Java �İ󶨡���������� XML ����
IllegalArgumentException - ����κβ���Ϊ null
�����°汾��ʼ��
JAXB 2.0

getUnmarshallerHandler

UnmarshallerHandler getUnmarshallerHandler()
��ȡ������ XML �����е������ unmarshaller �����������

JAXB �ṩ�߿���Ϊ�˷����Ķ�ε��÷�����ͬ�Ĵ���������󡣻��仰˵���˷������ش����µ� UnmarshallerHandler ʵ�������Ӧ�ó�����Ҫʹ�ö�� UnmarshallerHandler����ô��Ӧ�ô������ Unmarshaller��

���أ�
���� unmarshaller �����������
������
UnmarshallerHandler

setValidating

void setValidating(boolean validating)
                   throws JAXBException
�ѹ�ʱ�� �� JAXB2.0 ��ʼ������� setSchema(javax.xml.validation.Schema)

ָ�� Unmarshaller ��Ĭ����֤�����Ƿ�Ӧ�ڽ�������ڼ�ִ����֤��Ĭ������£�Unmarshaller ��ִ����֤��

�˷���ֻ���ڵ���ijһ unmarshal ����֮ǰ��֮����á�

�˷���ֻ���� JAXB �ṩ�ߵ�Ĭ�Ͻ���ʱ��֤���ƣ�����Ӱ��ָ���Լ�����֤���� SAX 2.0 �Ľ������Ŀͻ��ˡ�ָ���Լ��Ľ���ʱ��֤���ƵĿͻ��˿���ϣ��ͨ���� API �ر� JAXB �ṩ�ߵ�Ĭ����֤���ƣ��Ա���“˫����֤”��

�˷����� JAXB 2.0 ��ʼ�Ѿ���������ʹ���µ� setSchema(javax.xml.validation.Schema) API��

������
validating - ��� Unmarshaller Ӧ���ڽ����ڼ�ִ����֤����ò���Ϊ true������Ϊ false
�׳���
JAXBException - ����ڽ����ڼ����û������֤ʱ��������
UnsupportedOperationException - ����Ը������� JAXB 2.0 ӳ����� JAXBContext �������� Unmarshaller ���ô˷��������׳����쳣

isValidating

boolean isValidating()
                     throws JAXBException
�ѹ�ʱ�� �� JAXB 2.0 ��ʼ������� getSchema()

ָʾ�Ƿ� Unmarshaller ����Ϊ�ڽ�������ڼ�ִ����֤��

�� API ���� JAXB �ṩ�ߵ�Ĭ�Ͻ���ʱ��֤���Ƶ�״̬��

�˷����� JAXB 2.0 ��ʼ�Ѿ���������ʹ���µ� getSchema() API��

���أ�
����� Unmarshaller ����Ϊ�ڽ�������ڼ�ִ����֤���򷵻� true�����򷵻� false
�׳���
JAXBException - ����ڼ�����֤��־ʱ��������
UnsupportedOperationException - ����Ը������� JAXB ӳ����� JAXBContext �������� Unmarshaller ���ô˷��������׳����쳣

setEventHandler

void setEventHandler(ValidationEventHandler handler)
                     throws JAXBException
����Ӧ�ó���ע�� ValidationEventHandler��

����ڵ����κ� unmarshal �����ڼ������κδ����� ValidationEventHandler ���� JAXB �ṩ�ߵ��á�����ͻ���Ӧ�ó���û���ڵ��� unmarshal ����֮ǰע�� ValidationEventHandler���� ValidationEvents ����Ĭ���¼�������������Ĭ���¼�����������������һ���������������ʱ��ֹ���������

���ô��� null �����Ĵ˷��������� Unmarshaller ����ʹ��Ĭ���¼���������

������
handler - ��֤�¼���������
�׳���
JAXBException - ����������¼����������ڼ���������

getEventHandler

ValidationEventHandler getEventHandler()
                                       throws JAXBException
���ص�ǰ���¼������������û�������¼����������򷵻�Ĭ�ϵ��¼���������

���أ�
��ǰ�� ValidationEventHandler�����û�����ô��������򷵻�Ĭ�ϵ��¼���������
�׳���
JAXBException - �����ȡ��ǰ���¼���������ʱ��������

setProperty

void setProperty(String name,
                 Object value)
                 throws PropertyException
���� Unmarshaller �ײ�ʵ���е��ض����ԡ��˷���ֻ���������������б�׼ JAXB ���������֮һ���ض����ṩ�ߵ����ԡ���ͼ����δ��������Խ������׳� PropertyException�������֧�ֵ�������

������
name - Ҫ���õ����Ե����ơ���ֵ����ʹ��һ�������ֶ���ָ����Ҳ������һ���û��ṩ���ַ�����
value - Ҫ���õ�����ֵ
�׳���
PropertyException - ��������������Ի�ֵʱ��������
IllegalArgumentException - ��� name ����Ϊ null

getProperty

Object getProperty(String name)
                   throws PropertyException
��ȡ Unmarshaller �ײ�ʵ���е��ض����ԡ��˷���ֻ�����ڻ�ȡ�����б�׼ JAXB ���������֮һ���ض����ṩ�ߵ����ԡ���ͼ��ȡδ��������Խ������׳� PropertyException�������֧�ֵ�������

������
name - Ҫ���������Ե�����
���أ�
���������Ե�ֵ
�׳���
PropertyException - ��������������Ի�ֵ��������ʱ��������
IllegalArgumentException - ��� name ����Ϊ null

setSchema

void setSchema(Schema schema)
ָ��Ӧ������֤��������������ݵ� JAXP 1.3 Schema ������˷������� null ��������֤��

�˷������滻�ѹ�ʱ�� setValidating(boolean) API��

����������Ա�����Ϊ null��

������
schema - ��Ϊ��֤����������ݵ� Schema ����Ϊ null ��ʾ������֤
�׳���
UnsupportedOperationException - ����Ը������� JAXB 1.0 ӳ����� JAXBContext �������� Unmarshaller ���ô˷��������׳����쳣
�����°汾��ʼ��
JAXB 2.0

getSchema

Schema getSchema()
��ȡ����ִ�н���ʱ��֤�� JAXP 1.3 Schema �������û���� unmarshaller ������ Schema����˷��������� null��ָʾ����ִ�н���ʱ��֤��

�˷��������滻�ѹ�ʱ�� isValidating() API�����ɷ��� Schema ����Ҫȷ�� Unmarshaller �Ƿ���������֤��ֻ����Է��������Ƿ�Ϊ null��

boolean isValidating = u.getSchema()!=null;

���أ�
��������ִ�н���ʱ��֤�� Schema ��������ö��󲻴��ڣ��򷵻� null
�׳���
UnsupportedOperationException - ����Ը������� JAXB 1.0 ӳ����� JAXBContext �������� Unmarshaller ���ô˷��������׳����쳣
�����°汾��ʼ��
JAXB 2.0

setAdapter

void setAdapter(XmlAdapter adapter)
�������õ� XmlAdapter ʵ����� unmarshaller ������

���ǵ��� setAdapter(adapter.getClass(),adapter); ��һ����ݷ�����

�׳���
IllegalArgumentException - ��� adapter ����Ϊ null��
UnsupportedOperationException - ������� JAXB 1.0 ʵ�ֵ��á�
�����°汾��ʼ��
JAXB 2.0
������
setAdapter(Class,XmlAdapter)

setAdapter

<A extends XmlAdapter> void setAdapter(Class<A> type,
                                       A adapter)
�������õ� XmlAdapter ʵ����� unmarshaller ������

ÿ�� unmarshaller ���������ڲ�ά��һ�� Map<Class,XmlAdapter>������������ʹ�� XmlJavaTypeAdapter ע�����ֶ�/�������ࡣ

�˷�������Ӧ�ó���ʹ�������úõ� XmlAdapter ʵ�������û�и���������ʵ������ unmarshaller ��������Ĭ�Ϲ��췽������һ��ʵ����

������
type - �����������͡��� XmlJavaTypeAdapter.value() ���ô�����ʱ����ʹ��ָ����ʵ����
adapter - ��Ҫʹ�õ�������ʵ�������Ϊ null����ע�������͵ĵ�ǰ���������ϡ�
�׳���
IllegalArgumentException - ��� type ����Ϊ null��
UnsupportedOperationException - ������� JAXB 1.0 ʵ�ֵ��á�
�����°汾��ʼ��
JAXB 2.0

getAdapter

<A extends XmlAdapter> A getAdapter(Class<A> type)
��ȡ��ָ�����͹������������� ���� setAdapter(javax.xml.bind.annotation.adapters.XmlAdapter) �����ķ��������

�׳���
IllegalArgumentException - ��� type ����Ϊ null��
UnsupportedOperationException - ������� JAXB 1.0 ʵ�ֵ��á�
�����°汾��ʼ��
JAXB 2.0

setAttachmentUnmarshaller

void setAttachmentUnmarshaller(AttachmentUnmarshaller au)

������ cid������ id URI��������������Ϊ�������ݵĶ��������ݹ�����

ͨ�� setSchema(Schema) ���õĽ���ʱ��֤����ʹ�� unmarshaller ִ�� XOP ����ʱҲ����֧�֡�

�׳���
IllegalStateException - �����ͼ�ڽ�������ڼ�ͬʱ���ô˷�����

getAttachmentUnmarshaller

AttachmentUnmarshaller getAttachmentUnmarshaller()

setListener

void setListener(Unmarshaller.Listener listener)

��� Unmarshaller ע������¼��ص� Unmarshaller.Listener��

ÿ�� Unmarshaller ֻ��һ�� Listener������ Listener ���滻��ǰ���õ� Listener��ͨ���� listener ����Ϊ null ��ע����ǰ�� Listener��

������
listener - Ϊ�� Unmarshaller �ṩ�����¼��ص�
�����°汾��ʼ��
JAXB 2.0

getListener

Unmarshaller.Listener getListener()

������� Unmarshaller ע��� Unmarshaller.Listener��

���أ�
ע��� Unmarshaller.Listener�����δ��� Unmarshaller ע���κ� Listener���򷵻� null��
�����°汾��ʼ��
JAXB 2.0

JavaTM Platform
Standard Ed. 6

�ύ��������
�йظ���� API �ο����ϺͿ�����Ա�ĵ�������� Java SE ������Ա�ĵ������ĵ���������ϸ�ġ����򿪷���Ա���������Լ�������������ﶨ�塢ʹ�ü��ɺ͹�������ʾ����

��Ȩ���� 2007 Sun Microsystems, Inc. ��������Ȩ���� ����������֤��������������ĵ����·ַ�������