<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    閑人野居
    好好學習,天天向上
    posts - 57,  comments - 137,  trackbacks - 0
    ??? 讀取xml對于應用軟件來說是一個必不可少的工作,當然現在的jdk也提供了很好的處理xml方式,讀寫xml的庫也挺多,包括有名的dom4j,不管使用任何的代碼庫,對于xml只是一個解析工作而已,不能馬上綁定到java 對象。對于對象,每次都需要set 或者get相應的屬性,當然也可以使用map 來保存xml配置。
    ??? 于是,一種新的處理方式用于對象和xml之間的映射就變得非常需要,還好sun提供了jaxb,一種很方便的方式來處理java對象和xml內容。下面通過一個實例來體會一下。
    ??? 看一下如下的xml
    <?xml version="1.0"?>
    <customer id="No1">
    ??? <name>Alice Smith</name>
    ??? <address>
    ??? ??? <street>123 Maple Street</street>
    ??? ??? <city>Cambridge</city>
    ??? ??? <zip>12345</zip>
    ??? </address>
    </customer>

    別忘了生成相應的xsd,或者dtd文件,這是主要的配置:
    xsd:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ???
    ??? <xs:complexType name="Customer">
    ????? <xs:sequence>
    ??? ??? ?<xs:element name="address" type="Address"/>
    ???????? <xs:element name="name" type="xs:string"/>
    ??? ? </xs:sequence>
    ??? ?? <xs:attribute name="id" type="xs:string"/>??? ???
    ??? </xs:complexType>?
    ???
    ??? <xs:complexType name="Address">
    ????? <xs:sequence>
    ??? ??? ?<xs:element name="street" type="xs:string"/>
    ???????? <xs:element name="city" type="xs:string"/>
    ???????? <xs:element name="zip" type="ZipCodeType"/>
    ??? ?? </xs:sequence>
    ?? </xs:complexType>?
    ??
    ??? <xs:simpleType name="ZipCodeType">
    ????? <xs:restriction base="xs:integer">
    ???????? <xs:minInclusive value="10000"/>
    ???????? <xs:maxInclusive value="99999"/>
    ????? </xs:restriction>
    ??? </xs:simpleType>
    ??? <xs:element name="customer" type="Customer"/>
    ??? <xs:element name="address" type="Address"/>
    </xs:schema>


    需要映射兩個java對象,CustomerBo和AddressBo
    java 對象可以通過xjc來生成。
    或者自己定義(但需要增加相應的java注釋,如@XmlAccessorType,@XmlType,這是給引擎使用的)
    所以一般通過xjd自動生成


    @XmlAccessorType(AccessType.FIELD)
    @XmlType(name = "Customer", propOrder = {
    ??? "address",
    ??? "customerName"
    })
    public class CustomerBo {

    ??? protected Address address;

    ??? @XmlElement(name = "name")
    ??? protected String customerName;

    ??? @XmlAttribute
    ??? protected String id;

    ??? public Address getAddress() {
    ??????? return address;
    ??? }

    ??? public String getCustomerName() {
    ??????? return customerName;
    ??? }

    ??? public String getId() {
    ??????? return id;
    ??? }

    ??? public void setAddress(Address value) {
    ??????? this.address = value;
    ??? }

    ??? public void setCustomerName(String value) {
    ??????? this.customerName = value;
    ??? }

    ??? public void setId(String value) {
    ??????? this.id = value;
    ??? }
    }


    public class Address {

    ??? protected String street;

    ??? protected String city;

    ??? @XmlElement(name = "zip")
    ??? protected BigInteger zipCode;

    ??? public String getStreet() {
    ??????? return street;
    ??? }

    ??? public void setStreet(String value) {
    ??????? this.street = value;
    ??? }

    ??? public String getCity() {
    ??????? return city;
    ??? }

    ??? public void setCity(String value) {
    ??????? this.city = value;
    ??? }

    ??? public BigInteger getZipCode() {
    ??????? return zipCode;
    ??? }

    ??? public void setZipCode(BigInteger value) {
    ??????? this.zipCode = value;
    ??? }

    }

    定義jxb綁定文件:
    <jxb:bindings version="1.0"
    ?????????????? xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    ?????????????? xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ?? <jxb:bindings schemaLocation="customer.xsd" node="/xs:schema">???
    ??????????
    ?? <jxb:globalBindings
    ???????? fixedAttributeAsConstantProperty="false"
    ???????? collectionType="java.util.Vector"
    ???????? typesafeEnumBase="xs:NCName"
    ???????? choiceContentProperty="false"
    ???????? typesafeEnumMemberName="generateError"
    ???????? enableFailFastCheck="false"??
    ???????? generateIsSetMethod="false"
    ???????? underscoreBinding="asCharInWord"/>
    ?? <jxb:schemaBindings>
    ????? <jxb:package name="mycompany.demo">
    ??????? <jxb:javadoc><![CDATA[<body>Package level documentation for generated package mycompany.demo.</body>]]>
    ??? ??? </jxb:javadoc>
    ????? </jxb:package>
    ????? <jxb:nameXmlTransform>
    ??????? <jxb:elementName suffix="Element"/>
    ????? </jxb:nameXmlTransform>
    ??? </jxb:schemaBindings>??

    ??? //需要綁定的元素
    ?? <jxb:bindings node="http://xs:complexType[@name='Customer']">
    ??? ?? //綁定的類
    ????? <jxb:class name="CustomerBo">
    ??????? <jxb:javadoc>A &lt;b>todo..</jxb:javadoc>
    ????? </jxb:class>
    ????? <jxb:bindings node=".//xs:element[@name='name']">
    ?????????? //綁定的屬性
    ?????????? <jxb:property name="customerName"/>
    ??????? </jxb:bindings>
    ??? </jxb:bindings>??
    ?????????
    ??? <jxb:bindings node="http://xs:complexType[@name='Address']">
    ????? <jxb:class name="AddressBo">
    ??????? <jxb:javadoc><![CDATA[First line of documentation for a <b>Address</b>.]]></jxb:javadoc>
    ????? </jxb:class>
    ????? <jxb:bindings node=".//xs:element[@name='zip']">
    ???????? <jxb:property name="zipCode"/>
    ????? </jxb:bindings>
    ??? </jxb:bindings>?
    ????????????
    ?? </jxb:bindings>
    </jxb:bindings>

    看著比較復雜,其實挺好理解,當然可以不需要這個綁定文件,也可以綁定相應的java 類,但需要元素名稱和類名稱完全一致,而且屬性也要一致。

    看一下jaxb是如何來讀入xml的:
    ??? ??? //主要的環境類,主要讀取ObjectFactory這個類,這是由xjc生成的。
    ??????? JAXBContext jc = JAXBContext.newInstance("mycompany.demo");
    ??????? Unmarshaller u = jc.createUnmarshaller();
    ??????? JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream(
    ??????????????????????????????? "customer.xml"));
    ??????? CustomerBo bo = (CustomerBo) customerE.getValue();
    就是這么簡單

    寫入也比較簡單:
    ??????? JAXBContext jc = JAXBContext.newInstance("mycompany.demo");
    ??? ??? Marshaller marshaller=jc.createMarshaller();
    ??????? marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    ??????? customerE.setValue(bo);
    ??????? marshaller.marshal( customerE,new FileOutputStream("test.xml"));

    在webservices中jaxb的作用是明顯的,當然也有不方便的地方,比如定義binding.jaxb文件時,如果沒有工具支持,手工寫,還是比較困難。





    posted on 2006-11-11 20:20 布衣郎 閱讀(4393) 評論(1)  編輯  收藏 所屬分類: webservies

    FeedBack:
    # re: 從一個實例看jaxb的強大
    2007-03-02 17:47 | itVincent
    還要看輔助工具的發展,和性能如何  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    <2007年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357238
    • 排名 - 155

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲高清免费视频| 免费jjzz在在线播放国产| 无码专区AAAAAA免费视频| a视频免费在线观看| 中国videos性高清免费| 天堂在线免费观看| 99精品全国免费观看视频..| 99久久免费国产精品热| 最近的2019免费中文字幕| 在线观看肉片AV网站免费| 免费成人高清在线视频| 人妻无码一区二区三区免费| 最近中文字幕完整版免费高清| 69av免费视频| 福利免费观看午夜体检区| 女人张开腿等男人桶免费视频| 成人永久福利免费观看| 亚洲国产婷婷综合在线精品| 国产偷国产偷亚洲高清日韩| 久久被窝电影亚洲爽爽爽| 亚洲一本综合久久| 亚洲娇小性xxxx| 在线视频亚洲一区| 久久久WWW免费人成精品| 亚洲精品免费视频| 在线观看特色大片免费视频| 国产成人在线免费观看| 亚洲一区视频在线播放| 亚洲va在线va天堂va888www| 67194在线午夜亚洲| 美女被羞羞网站免费下载| 99久久免费国产精精品| 成视频年人黄网站免费视频| 国产男女猛烈无遮挡免费视频| 精品亚洲视频在线观看| 亚洲性无码av在线| 阿v免费在线观看| 久久99热精品免费观看动漫| 毛片免费观看的视频在线| 亚洲美日韩Av中文字幕无码久久久妻妇| 精品久久久久久亚洲|