乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      WebService CXF學(xué)習(xí)(進(jìn)階篇4):JAXB剖析

       昵稱21964271 2015-02-14

      前面幾節(jié)我們講解對象傳遞,但是通常情況下我們不直接傳對象,因?yàn)橹苯觽鬟f對象安全性差,而且暴露了實(shí)體對象。所以我們選擇傳遞XML文件,當(dāng)然也可以傳遞JSON對象。這節(jié)我只針對傳遞XML,那么JAVA綁定成XML,服務(wù)端將XML解析成Java對象有什么工具可用嗎,其實(shí)這樣的工具多的是。這里我選擇一個(gè)比較簡單的JAXB工具來講解一下。 
          JAXB(Java Architecture for XML Binding)提供了一個(gè)快速而方便的方式綁定XML Schemas和java,使java程序員能夠很方便的在java應(yīng)用程序中處理XML數(shù)據(jù)。JAXB提供了將XML文檔解組為java內(nèi)容樹的方法,以及將java內(nèi)容樹重新編組回XML文檔的方法。JAXB同樣也提供了一種從java對象生成XML Schema的方式。 
          這里有幾個(gè)重要的定義: 
          編組(Marshalling)是把內(nèi)存中的數(shù)據(jù)轉(zhuǎn)化到存儲(chǔ)媒介上的過程。因此在 Java 和 XML 環(huán)境中,編組就是把一些 Java 對象轉(zhuǎn)化成一個(gè)(或多個(gè)) XML 文檔。在數(shù)據(jù)庫環(huán)境中,則是把 Java 表示的數(shù)據(jù)存入數(shù)據(jù)庫。顯然,編組的秘密在于把 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í)際上很簡單)。工作原理如下圖所示: 

       

          解組(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ù)語。往返用于描述從存儲(chǔ)媒介到內(nèi)存然后回到存儲(chǔ)媒介的完整循 環(huán)。在 XML 和 Java 技術(shù)環(huán)境中,這就意味著從 XML 文檔到 Java 實(shí)例變量,然后再回到 XML 文檔。正確的往返要求,如果中間沒有修改數(shù)據(jù),XML 輸入和 XML 輸出應(yīng)該是等同的。 
          下載地址:http://java./developer/technicalArticles/WebServices/jaxb/ 
         
          我們還以例子來說明它的工作原理,直觀點(diǎn)。 
          第一步,創(chuàng)建一個(gè)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)建一個(gè)測試類 
      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. }  


            第三步,運(yùn)行一個(gè)測試類,看看效果如何。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>  

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多