xmlBean學習一,xmlBean學習

來源:互聯網
上載者:User

xmlBean學習一,xmlBean學習

在文檔中看到了xmlBean的出現,因為項目使用JMS,模組之間通過xml檔案傳遞資料,就學一下xmlBean,java中還提供了DOM,SAX來解析xm,但也是比較麻煩的;而xmlbean則將xml轉化為javabean,簡化了xml的讀寫。好了,下面講解一下xmlBean的使用:

首先,下載xmlbean,apache下的一個開源項目,:http://archive.apache.org/dist/xml/xmlbeans/xmlbeans-current.zip,最新版本是2.6。

解壓後目錄是:

  xmlbeans-1.0.3

    --bin

    --docs

    --lib

    --schemas

    --src

第二:配置環境變數,配置path路徑(E:\xmlbeans-1.0.3\bin)。

第三:建立一個xml檔案,(customers.xml),內容是直接在網上找的,

<?xml version="1.0" encoding="UTF-8"?><Customers>    <customer>        <id>1</id>        <gender>female</gender>        <firstname>Jessica</firstname>        <lastname>Lim</lastname>        <phoneNumber>1234567</phoneNumber>        <address>            <primaryAddress>                <postalCode>350106</postalCode>                <addressLine1>#25-1</addressLine1>                <addressLine2>SHINSAYAMA 2-CHOME</addressLine2>            </primaryAddress>            <billingAddress>                <receiver>Ms Danielle</receiver>                <postalCode>350107</postalCode>                <addressLine1>#167</addressLine1>                <addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>            </billingAddress>        </address>    </customer>    <customer>        <id>2</id>        <gender>male</gender>        <firstname>David</firstname>        <lastname>Bill</lastname>        <phoneNumber>808182</phoneNumber>        <address>            <primaryAddress>                <postalCode>319087</postalCode>                <addressLine1>1033 WS St.</addressLine1>                <addressLine2>Tima Road</addressLine2>            </primaryAddress>            <billingAddress>                <receiver>Mr William</receiver>                <postalCode>672993</postalCode>                <addressLine1>1033 WS St.</addressLine1>                <addressLine2>Tima Road</addressLine2>            </billingAddress>        </address>    </customer></Customers>

  這是一個客戶的資料模型,每個客戶都有客戶編碼(ID),姓名,性別(gender),電話號碼(phoneNumber)和地址,其中地址有兩個: 首要地址(PrimaryAddress)和帳單地址(BillingAddress),每個地址有郵編,地址1,和地址2組成.其中帳單地址還有收件者(receiver).

第四:使用步驟:

1.產生XML Schema檔案

正常情況下,每個xml檔案都有一個Schema檔案,他是一個xml的約束條件,定義了xml檔案的結構元素,以及對元素結構的約束。

xmlbean需要通過這個檔案知道xml檔案的結構約束,比如資料類型等,利用這個檔案,XMLBean將會產生一系列相關的java calsses來實現對xm的操作,開發人員利用這些java classess來完成對xml的操作。

可以使用XMLSPY和Stylus Studio通過xml檔案來產生Schema檔案。加入customer.xml的Schema檔案(sustomer.xsd)

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  
    <xs:element name="Customers">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="customer"
                    type="customerType" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="customerType">
        <xs:sequence>
            <xs:element name="id" type="xs:int" />
            <xs:element name="gender" type="xs:string" />
            <xs:element name="firstname" type="xs:string" />
            <xs:element name="lastname" type="xs:string" />
            <xs:element name="phoneNumber" type="xs:string" />
            <xs:element name="address" type="addressType" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="addressType">
        <xs:sequence>
            <xs:element name="primaryAddress" type="primaryAddressType" />
            <xs:element name="billingAddress" type="billingAddressType" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="primaryAddressType">
        <xs:sequence>
            <xs:element name="postalCode" type="xs:string" />
            <xs:element name="addressLine1" type="xs:string" />
            <xs:element name="addressLine2" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="billingAddressType">
        <xs:sequence>
            <xs:element name="receiver" type="xs:string" />
            <xs:element name="postalCode" type="xs:string" />
            <xs:element name="addressLine1" type="xs:string" />
            <xs:element name="addressLine2" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

 

2.利用scomp來產生java classes

scomp是XMLBean提供的一個編譯工具,它在bin的目錄下. 通過這個工具,我們可以將以上的Schema檔案產生Java Classes.scomp的文法如下:-
  scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*
  主要參數說明:
  -src [dir] -- 產生的Java Classes存放目錄
  -srconly -- 不編譯Java Classes,不產生Jar檔案
  -out [jarFileName] -- 產生的Jar檔案,預設是xmltypes.jar
  -compiler -- Java編譯器的路徑,即Javac的位置
  schemaFile.xsd -- XML Schema檔案位置
    config.xsdconfig -- xsdconfig檔案的位置, 這個檔案主要用來制定產生的Java Class的一些檔案名稱規則和Package的名稱,在本文,package是sample.xmlbean
準備一個設定檔(檔案名稱customer.xsdconfig),它的內容如下:
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">

  <xb:namespace>
    <xb:package>sample.xmlbean</xb:package>
  </xb:namespace>

</xb:config>

 

我的運行命令是這樣的:

E:\xmlbeans-1.0.3>scomp -src build/src -out E:/xmlbeans-1.0.3/customerXmlBean.jar schemas/customer/customer.xsd -compiler C:/"Program Files"/Javak1.7.0_04/bin/javac E:/xmlbeans-1.0.3/schemas/custom er/customer.xsdconfig 這個命令是告訴scomp,將產生的源碼放到E:\xmlbeans-1.0.3下的build裡;將產生customerXmlBean.jar 放到E:/xmlbeans-1.0.3的根目錄下,建議此路徑寫絕對路徑;Schame檔案是customer.xsd檔案,xsdconfig檔案是customer.xsdconfig檔案,我們主要用的是customerXmlBean.jar 。在src下的源碼:CustomersDocument.java -- 整個XML文檔的Java Class映射
  CustomerType.java -- 節點sustomer的映射
  AddressType.java -- 節點address的映射
  BillingAddressType.java -- 節點billingAddress的映射
  PrimaryAddressType.java -- 節點primaryAddress的映射
  注意:scomp命令中,空格是分割不同的參數的,所以參數裡的檔案路徑包括空格的要用引號括起來。

準備工作就這些,在下一篇會介紹具體的如何使用xmlbean來進行對xml的讀寫操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.