Jibx簡單示例

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
          首先從 JiBX 網站下載 JiBX,當前最新版本是 beta 3。解開下載的 zip 文件,里面有一個 lib 目錄,包含了 bcel.jar, jibx-bind.jar, jibx-extras.jar, jibx-run.jar, xpp3.jar 五個 jar 文件。bcel.jar, jibx-bind.jar 只有在 binding compiler 的時候才用得到。jibx-extras.jar 是一個可選的工具包,里面有一些測試和驗證的工具類。 
          1.定義一個我們將要處理 XML 文件,文件名為 data.xml,內容如下: 

          <customer> 
          <person> 
            
          <cust-num>123456789</cust-num> 
            
          <first-name>John</first-name> 
            
          <last-name>Smith</last-name> 
          </person> 
          <street>12345 Happy Lane</street> 
          <city>Plunk</city> 
          <state>WA</state> 
          <zip>98059</zip> 
          <phone>888.555.1234</phone> 
          </customer> 
          這個 XML 文件非常簡單,共有十個元素,沒有屬性。根元素 customer 有 person, street, city, state, zip, phone 六個子元素。其中元素 person 有 cust-num, first-name, last-name 三個子元素。 
          2.接著定義兩個 Java 類 Customer 和 Person,也采用最簡單的方式,用對象的域值對應元素,內容如下: 

          public class Customer { 
            
          public Person person; 
            
          public String street; 
            
          public String city; 
            
          public String state; 
            
          public Integer zip; 
            
          public String phone; 


          public class Person { 
            
          public int customerNumber; 
            
          public String firstName; 
            
          public String lastName; 
          這個兩個類沒有任何方法,夠簡單吧!或許你已經看出來了,Customer 類的七個 field 對應的是 XML 文件中 customer 元素的七個子元素。Person 類的三個 field 對應的是 person 元素的三個子元素。在 Person 類的 field 的名稱并不是和 person 元素的子元素名稱完全相等,這是遵守 Java 編程規范 field 命名的需要,雖然不相等,但這不重要,可以在綁定定義文擋中把它們一一對應起來。 
          3.綁定定義文擋 
          綁定定義文擋是依據綁定定義規范將 XML 數據和 Java 對象綁定的 XML 文擋。文件名為 binding.xml,內容如下: 


          binding.xml 文件中的 name 和 field 屬性分別將 XML 中的元素和 Java 對象中的 field 一一對應并綁定起來。 

          <mapping name="customer" class="Customer"> 
          mapping 元素的 name 和 class 屬性將 customer 根元素和 Customer 類綁定在一起。 

          <structure name="person" field="person"> 

          public Person person; 
          上面兩行定義了 person 是 Customer 的 field,同時也把 person 元素和 person 類綁定在一起。 

          <binding> 
          <mapping name="customer" class="Customer"> 
            
          <structure name="person" field="person"> 
             
          <value name="cust-num" field="customerNumber"/> 
             
          <value name="first-name" field="firstName"/> 
             
          <value name="last-name" field="lastName"/> 
            
          </structure> 
            
          <value name="street" field="street"/> 
            
          <value name="city" field="city"/> 
            
          <value name="state" field="state"/> 
            
          <value name="zip" field="zip"/> 
            
          <value name="phone" field="phone"/> 
          </mapping> 
          </binding> 
          4.執行 Binding Compiler 過程 
          以下命令是在 Linux 下執行,如果是 Windows 平臺請轉換成相應的命令 

          #javac Person.java 
          #javac 
          -classpath . Customer.java 
          #java 
          -jar lib/jibx-bind.jar binding.xml 
          執行完后,在當前目錄多了四個 class 文件,分別是 Person.class, Customer.class, JiBX_bindingCustomer_access.class, JiBX_bindingFactory.class。 
          5.執行 binding runtime 過程 
          接著寫一個簡單的讀取 data.xml 測試程序 Test.java,內容如下: 

          import java.io.FileInputStream; 
          import java.io.FileNotFoundException; 

          import org.jibx.runtime.JiBXException; 
          import org.jibx.runtime.IBindingFactory; 
          import org.jibx.runtime.BindingDirectory; 
          import org.jibx.runtime.IUnmarshallingContext; 

          class Test { 
            
          public static void main(String[] args) { 
            
          try
              IBindingFactory bfact 
          = BindingDirectory.getFactory(Customer.class); 
              IUnmarshallingContext uctx 
          = bfact.createUnmarshallingContext(); 
              Customer customer 
          = (Customer)uctx.unmarshalDocument(new FileInputStream("data.xml"), null); 
              Person person 
          = customer.person; 

              System.out.println(
          "cust-num:" + person.customerNumber); 
              System.out.println(
          "first-name:" + person.firstName); 
              System.out.println(
          "last-name:" + person.lastName); 
              System.out.println(
          "street:" + customer.street); 
            }
          catch(FileNotFoundException e){ 
              System.out.println(e.toString()); 
            }
          catch(JiBXException e){ 
              System.out.println(e.toString()); 
            } 

          編譯并運行這個測試程序 

          #javac -classpath .:lib/jibx-run.jar Test.java 
          #java 
          -cp .:lib/jibx-run.jar:lib/xpp3.jar Test 
          程序運行的結果是 

          cust-num:123456789 
          first
          -name:John 
          last
          -name:Smith 
          street:
          12345 Happy Lane  


          Kyle Wang

          posted on 2011-11-28 17:16 王樹東 閱讀(2536) 評論(0)  編輯  收藏 所屬分類: Java Skills Learning and Sharing 、Code Templates

          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          統計

          公告

          常用鏈接

          留言簿

          隨筆分類(17)

          隨筆檔案(15)

          文章分類(4)

          文章檔案(5)

          收藏夾(4)

          Algorithm

          Design

          Environment Setup

          Installer

          Maven

          MINA

          OS

          Skills for Java

          VIM

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 饶阳县| 永吉县| 施秉县| 珲春市| 习水县| 电白县| 泸定县| 永胜县| 富宁县| 水富县| 炎陵县| 德安县| 乌拉特中旗| 宁南县| 永仁县| 高青县| 沧州市| 天柱县| 海丰县| 溧阳市| 定结县| 庐江县| 海原县| 都昌县| 共和县| 遂昌县| 新疆| 丁青县| 肥东县| 南部县| 广西| 台州市| 江油市| 新竹市| 吉林市| 岳西县| 丰都县| 得荣县| 临高县| 永兴县| 桑植县|