前面幾節(jié)我們講解對象傳遞,但是通常情況下我們不直接傳對象,因為直接傳遞對象安全性差,而且暴露了實體對象。所以我們選擇傳遞XML文件,當然也可以傳遞JSON對象。這節(jié)我只針對傳遞XML,那么JAVA綁定成XML,服務端將XML解析成Java對象有什么工具可用嗎,其實這樣的工具多的是。這里我選擇一個比較簡單的JAXB工具來講解一下。 JAXB(Java Architecture for XML Binding)提供了一個快速而方便的方式綁定XML Schemas和java,使java程序員能夠很方便的在java應用程序中處理XML數(shù)據(jù)。JAXB提供了將XML文檔解組為java內(nèi)容樹的方法,以及將java內(nèi)容樹重新編組回XML文檔的方法。JAXB同樣也提供了一種從java對象生成XML Schema的方式。 這里有幾個重要的定義: 編組(Marshalling)是把內(nèi)存中的數(shù)據(jù)轉(zhuǎn)化到存儲媒介上的過程。因此在 Java 和 XML 環(huán)境中,編組就是把一些 Java 對象轉(zhuǎn)化成一個(或多個) XML 文檔。在數(shù)據(jù)庫環(huán)境中,則是把 Java 表示的數(shù)據(jù)存入數(shù)據(jù)庫。顯然,編組的秘密在于把 Java 實例中的面向?qū)ο蠼Y(jié)構(gòu)轉(zhuǎn)化成適用于 XML 的 扁平結(jié)構(gòu),或者 RDBMS 中的關系結(jié)構(gòu)(使用 Java 技術轉(zhuǎn)換到 OODBMS 實際上很簡單)。工作原理如下圖所示:
解組(Unmarshalling) 是把數(shù)據(jù)從存儲媒介轉(zhuǎn)換到內(nèi)存中的過程--正好與編組相反。因此需要把 XML 文檔解組到 Java VM 中。這里的復雜性不是在扁平數(shù)據(jù)中,因為這不是必需的,而在于從正確的數(shù)據(jù)到正確的 Java 代碼變量的映射。如果映射是錯誤的,就不可能正確地訪問數(shù)據(jù)。當然,如果再嘗試重新編組還會造成更大的問題,并且問題傳播得很快。工作原理如下圖所示:
往返(Round-tripping)可能是最重要也最容易誤解的數(shù)據(jù)綁定術語。往返用于描述從存儲媒介到內(nèi)存然后回到存儲媒介的完整循 環(huán)。在 XML 和 Java 技術環(huán)境中,這就意味著從 XML 文檔到 Java 實例變量,然后再回到 XML 文檔。正確的往返要求,如果中間沒有修改數(shù)據(jù),XML 輸入和 XML 輸出應該是等同的。 下載地址:http://java./developer/technicalArticles/WebServices/jaxb/ 我們還以例子來說明它的工作原理,直觀點。 第一步,創(chuàng)建一個Customer對象 - @XmlRootElement(name="customer")
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "")
- public class Customer {
-
- @XmlAttribute(required = true)
- protected String name;
- @XmlAttribute(required = true)
- protected int age;
-
-
-
-
-
-
-
-
-
- public String getName() {
- return name;
- }
-
-
-
-
-
-
-
-
-
- public void setName(String value) {
- this.name = value;
- }
-
-
-
-
-
- public int getAge() {
- return age;
- }
-
-
-
-
-
- public void setAge(int value) {
- this.age = value;
- }
-
- }
-
-
第二步,創(chuàng)建一個測試類
- public class SoapClient {
-
- private final static String MODEL = "com.itdcl.model";
- public static void main(String[] args) throws ParserConfigurationException, JAXBException, TransformerException{
-
-
- ObjectFactory factory = new ObjectFactory();
- Customer customer = factory.createCustomer();
- customer.setAge(20);
- customer.setName("Josen");
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
-
- JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
-
- Marshaller marshaller = jaxbContext.createMarshaller();
- marshaller.marshal(customer, doc);
-
- DOMSource domSource = new DOMSource(doc);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String xmlString = writer.toString();
- System.out.println(xmlString);
-
- Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
- StringReader reader = new StringReader(xmlString);
- Customer cus = (Customer)unmarshaller.unmarshal(reader);
- System.out.println("Age:"+cus.getAge());
- System.out.println("Name:"+cus.getName());
-
-
- }
- }
第三步,運行一個測試類,看看效果如何。Java與XML之間轉(zhuǎn)換如此簡單
編組操作:利用上面生成的java文件執(zhí)行編組操作。
- JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
-
- Marshaller marshaller = jaxbContext.createMarshaller();
- marshaller.marshal(customer, doc);
-
- DOMSource domSource = new DOMSource(doc);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String xmlString = writer.toString();
- System.out.println(xmlString);
解組操作:通過xml文件執(zhí)行解組操作。 - JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
- Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
- StringReader reader = new StringReader(xmlString);
- Customer cus = (Customer)unmarshaller.unmarshal(reader);
- System.out.println("Age:"+cus.getAge());
- System.out.println("Name:"+cus.getName());
也可通過Ant配置來解組,如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <project default="xjc-compile" basedir=".">
- <property name="src.dir" location="src" />
- <property name="lib.dir" location="E:/cxf-lib" />
- <property name="xml-schema.dir" location="src/WEB-INF" />
- <property name="schema.name" value="cxfdemo.xsd" />
- <property name="package" value="com.itdcl.model" />
-
- <path id="classpath">
- <fileset dir="${lib.dir}" includes="*.jar" />
- </path>
- <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
- classpathref="classpath" />
-
- <target name="xjc-compile">
- <echo message="Build Jaxb Class from Schema" />
- <xjc schema="${xml-schema.dir}/${schema.name}"
- destdir="${src.dir}" package="${package}" >
- <produces dir="src/com/itdcl/model" includes="*" />
- </xjc>
- </target>
- </project>
|