日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

WebService CXF學習(進階篇4):JAXB剖析

 昵稱21964271 2015-02-14

前面幾節(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對象 
   

Java代碼 
  1. @XmlRootElement(name="customer")  
  2. @XmlAccessorType(XmlAccessType.FIELD)  
  3. @XmlType(name = "")  
  4. public class Customer {  
  5.   
  6.     @XmlAttribute(required = true)  
  7.     protected String name;  
  8.     @XmlAttribute(required = true)  
  9.     protected int age;  
  10.   
  11.     /** 
  12.      * Gets the value of the name property. 
  13.      *  
  14.      * @return 
  15.      *     possible object is 
  16.      *     {@link String } 
  17.      *      
  18.      */  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.   
  23.     /** 
  24.      * Sets the value of the name property. 
  25.      *  
  26.      * @param value 
  27.      *     allowed object is 
  28.      *     {@link String } 
  29.      *      
  30.      */  
  31.     public void setName(String value) {  
  32.         this.name = value;  
  33.     }  
  34.   
  35.     /** 
  36.      * Gets the value of the age property. 
  37.      *  
  38.      */  
  39.     public int getAge() {  
  40.         return age;  
  41.     }  
  42.   
  43.     /** 
  44.      * Sets the value of the age property. 
  45.      *  
  46.      */  
  47.     public void setAge(int value) {  
  48.         this.age = value;  
  49.     }  
  50.   
  51. }  
  52.   
  53.       


    第二步,創(chuàng)建一個測試類 
Java代碼 
  1. public class SoapClient {  
  2.   
  3.     private final static String MODEL = "com.itdcl.model";  
  4.     public static void main(String[] args) throws ParserConfigurationException, JAXBException, TransformerException{  
  5.   
  6.           
  7.         ObjectFactory factory = new ObjectFactory();  
  8.         Customer customer = factory.createCustomer();  
  9.         customer.setAge(20);  
  10.         customer.setName("Josen");  
  11.   
  12.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
  13.         dbf.setNamespaceAware(true);  
  14.         DocumentBuilder db = dbf.newDocumentBuilder();  
  15.         Document doc = db.newDocument();  
  16.   
  17.         JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);  
  18.         //Java對象轉(zhuǎn)換成XML  
  19.                 Marshaller marshaller = jaxbContext.createMarshaller();  
  20.         marshaller.marshal(customer, doc);  
  21.           
  22.         DOMSource domSource = new DOMSource(doc);  
  23.         StringWriter writer = new StringWriter();  
  24.         StreamResult result = new StreamResult(writer);  
  25.         TransformerFactory tf = TransformerFactory.newInstance();  
  26.         Transformer transformer = tf.newTransformer();  
  27.         transformer.transform(domSource, result);  
  28.         String xmlString = writer.toString();  
  29.         System.out.println(xmlString);  
  30.         //XML轉(zhuǎn)換成Java對象  
  31.         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
  32.         StringReader reader = new StringReader(xmlString);  
  33.         Customer cus = (Customer)unmarshaller.unmarshal(reader);  
  34.         System.out.println("Age:"+cus.getAge());  
  35.         System.out.println("Name:"+cus.getName());  
  36.           
  37.           
  38.     }  
  39. }  


      第三步,運行一個測試類,看看效果如何。Java與XML之間轉(zhuǎn)換如此簡單 

      編組操作:利用上面生成的java文件執(zhí)行編組操作。 
Java代碼 
  1. JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);  
  2. //Java對象轉(zhuǎn)換成XML  
  3.               Marshaller marshaller = jaxbContext.createMarshaller();  
  4. marshaller.marshal(customer, doc);  
  5.   
  6. DOMSource domSource = new DOMSource(doc);  
  7. StringWriter writer = new StringWriter();  
  8. StreamResult result = new StreamResult(writer);  
  9. TransformerFactory tf = TransformerFactory.newInstance();  
  10. Transformer transformer = tf.newTransformer();  
  11. transformer.transform(domSource, result);  
  12. String xmlString = writer.toString();  
  13. System.out.println(xmlString);  


     解組操作:通過xml文件執(zhí)行解組操作。 
   
Java代碼 
  1.               JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);  
  2.               Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
  3. StringReader reader = new StringReader(xmlString);  
  4. Customer cus = (Customer)unmarshaller.unmarshal(reader);  
  5. System.out.println("Age:"+cus.getAge());  
  6. System.out.println("Name:"+cus.getName());  


    也可通過Ant配置來解組,如下: 
Java代碼 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <project default="xjc-compile" basedir=".">  
  3.     <property name="src.dir" location="src" />  
  4.     <property name="lib.dir" location="E:/cxf-lib" />  
  5.     <property name="xml-schema.dir" location="src/WEB-INF" />  
  6.     <property name="schema.name" value="cxfdemo.xsd" />  
  7.     <property name="package" value="com.itdcl.model" />  
  8.   
  9.     <path id="classpath">  
  10.         <fileset dir="${lib.dir}" includes="*.jar" />  
  11.     </path>  
  12.     <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"  
  13.         classpathref="classpath" />  
  14.           
  15.     <target name="xjc-compile">  
  16.         <echo message="Build Jaxb Class from Schema" />  
  17.         <xjc schema="${xml-schema.dir}/${schema.name}"  
  18.             destdir="${src.dir}" package="${package}" >  
  19.             <produces dir="src/com/itdcl/model" includes="*" />  
  20.         </xjc>  
  21.     </target>  
  22. </project>  

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多