JAXBJava Architecture for XML Binding)提供了一個(gè)快速而方便的方式綁定XML Schemasjava,使java程序員能夠很方便的在java應(yīng)用程序中處理XML數(shù)據(jù)。JAXB提供了將XML文檔解組為java內(nèi)容樹的方法,以及將java內(nèi)容樹重新編組回XML文檔的方法。JAXB同樣也提供了一種從java對(duì)象生成XML Schema的方式。

這里有幾個(gè)重要的定義:

Ø       編組(Marshalling是把內(nèi)存中的數(shù)據(jù)轉(zhuǎn)化到存儲(chǔ)媒介上的過程。因此在 Java XML 環(huán)境中,編組就是把一些 Java 對(duì)象轉(zhuǎn)化成一個(gè)(或多個(gè)) XML 文檔。在數(shù)據(jù)庫(kù)環(huán)境中,則是把 Java 表示的數(shù)據(jù)存入數(shù)據(jù)庫(kù)。顯然,編組的秘密在于把 Java 實(shí)例中的面向?qū)ο蠼Y(jié)構(gòu)轉(zhuǎn)化成適用于 XML 扁平結(jié)構(gòu),或者 RDBMS 中的關(guān)系結(jié)構(gòu)(使用 Java 技術(shù)轉(zhuǎn)換到 OODBMS 實(shí)際上很簡(jiǎn)單)。

Ø       解組(Unmarshalling 是把數(shù)據(jù)從存儲(chǔ)媒介轉(zhuǎn)換到內(nèi)存中的過程--正好與編組相反。因此需要把 XML 文檔解組到 Java VM 中。這里的復(fù)雜性不是在扁平數(shù)據(jù)中,因?yàn)檫@不是必需的,而在于從正確的數(shù)據(jù)到正確的 Java 代碼變量的映射。如果映射是錯(cuò)誤的,就不可能正確地訪問數(shù)據(jù)。當(dāng)然,如果再嘗試重新編組還會(huì)造成更大的問題,并且問題傳播得很快。

Ø       往返Round-tripping)可能是最重要也最容易誤解的數(shù)據(jù)綁定術(shù)語(yǔ)。往返用于描述從存儲(chǔ)媒介到內(nèi)存然后回到存儲(chǔ)媒介的完整循 環(huán)。在 XML Java 技術(shù)環(huán)境中,這就意味著從 XML 文檔到 Java 實(shí)例變量,然后再回到 XML 文檔。正確的往返要求,如果中間沒有修改數(shù)據(jù),XML 輸入和 XML 輸出應(yīng)該是等同的。

生成Java文件:

1.       可以通過JAXBbin中的xjc執(zhí)行生成操作,命令如下:

xjc –p com.chris.jaxb test.xsd –d gen-src

-p <pkg>:指定生成java文件的package

-d <dir>:指定生成java文件的目錄

其中-d所指定的目錄必須是已經(jīng)存在的,否則會(huì)報(bào):“cowardly refuses to write to a non-existent directory "gen-src"”。

2.        使用ant生成

ant文件中定義一個(gè)xjctask,通過執(zhí)行該task生成java文件,ant文件的定義:

<property name="jaxb.home" value="./lib" />
   <path id="classpath">
   <pathelement path="src" />
   <fileset dir="${jaxb.home}" includes="*.jar" />
</path>

<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
    <classpath refid="classpath" />
</taskdef>

<target name="generate" description="generate Java source files">
    <echo message="generate java from schema..." />
    <mkdir dir="gen-src" />
    <xjc schema="test.xsd" package="com.chris.jaxb" destdir="gen-src">
        <produces dir="gen-src/com.chris.jaxb " includes="**/*.java" />
    </xjc>
</target>

編組操作:

利用上面生成的java文件執(zhí)行編組操作。

JAXBContext context = JAXBContext.newInstance("com.chris.jaxb");

//create java content tree
PurchaseOrderType po = new PurchaseOrderType();
………..
// create an element for marshalling
JAXBElement<PurchaseOrderType> poElement = (new ObjectFacory()).createPurchaseOrder(po);

// create a Marshaller and marshal to System.out
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(poElement, System.out);

解組操作:

   通過xml文件執(zhí)行解組操作。

JAXBContext context = JAXBContext.newInstance("com.chris.jaxb");
Unmarshaller um = context.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema sc = sf.newSchema(Main.class.getResource("test.xsd"));
um.setSchema(sc);
JAXBElement<?> poEle = (JAXBElement<?>) um.unmarshal(new FileInputStream("po.xml"));
PurchaseOrderType pot = (PurchaseOrderType) poElement.getValue();

JAXB常用操作基本就這些,經(jīng)過綁定xmljava對(duì)象可以很方便的執(zhí)行一些xml操作,現(xiàn)在的一些webservice框架也都采用了JAXB作為綁定方式,所以JAXB的基本原理還是很有必要了解下的。