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

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

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

    andyj2ee

    java tec sky

    統(tǒng)計

    留言簿(4)

    activemq

    aop

    design pattern

    other blog

    spring

    workflow

    多線程

    軟件架構(gòu)師

    閱讀排行榜

    評論排行榜

    使用XMLBeans 進(jìn)行java 數(shù)據(jù)梆定

    進(jìn)入實(shí)踐之前,最好找篇文章先了解一下 xmlbeans 是用來做什么的
    參考資料:http://xmlbeans.apache.org/
    參考文章:XML-Java Data Binding Using XMLBeans

    xmlbeans bea 貢獻(xiàn)的一個開源項(xiàng)目,目的是使xml 文件直接轉(zhuǎn)化為java 對象,對java 對象的操作可直接存為xml 文件,省去了文檔驗(yàn)證過程。
    實(shí)現(xiàn)原理:先定義 xml schema descripter ,編譯定義的 *.xsd 文件,引入編譯后生成的.jar 包。與 xmlbeans-Home\lib\xbean.jar  就可以在開發(fā)項(xiàng)目中使用了。

    1. Install XMLBeans.
    2. Compile your schema. Use scomp to compile the schema, generating and jarring Java types. For example, to create a employeeschema.jar from an employeesschema.xsd file:
      scomp -out employeeschema.jar employeeschema.xsd
    3. Write code. With the generated JAR on your classpath, write code to bind an XML instance to the Java types representing your schema. Here's an example that would use types generated from an employees schema:
      File xmlFile = new File("c:\employees.xml"); 
      
      // Bind the instance to the generated XMLBeans types.
      EmployeesDocument empDoc = 
      	EmployeesDocument.Factory.parse(xmlFile); 
      
      // Get and print pieces of the XML instance.
      Employees emps = empDoc.getEmployees(); 
      Employee[] empArray = emps.getEmployeeArray(); 
      for (int i = 0; i < empArray.length; i++) 
      { 
      	System.out.println(empArray[i]); 
      }
    實(shí)踐中遇到的問題:
    如何修改編譯shema 產(chǎn)生的java 類的package 目錄結(jié)構(gòu)?


    參考:http://wiki.apache.org/xmlbeans/XmlBeansFaq#configPackageName

    Can I change the default package names for the java classes generated from my schema?

    You can create a file that ends in .xsdconfig to map targetnamespace to packagename. Put the .xsdconfig file in the same directory as the .xsd that you are compiling. Here is an example .xsdconfig:

    <!-- An xsdconfig file must begin with a "config" element in the 
            http://www.bea.com/2002/09/xbean/config namespace. Also, be sure
            to declare any namespaces used to qualify types in your schema (here,
            the namespace corresponding to the pol prefix. -->
    <xb:config xmlns:pol="http://openuri.org/easypoLocal" 
        xmlns:xb="http://www.bea.com/2002/09/xbean/config">
    
        <!-- Use the "namespace" element to map a namespace to the Java package
            name that should be generated. -->
        <xb:namespace uri="http://openuri.org/easypoLocal">
            <xb:package>org.openuri.easypo.xsdconfig</xb:package>
        </xb:namespace>
    
        <!-- Use the "qname" element to map schema type names to generated
            Java type names. In these examples, the name attribute's value is the
            XML element name; the javaname attribute's value is the Java type 
            that should be generated. -->
        <xb:qname name="pol:CUST" javaname="Customer"/>
        <xb:qname name="pol:PURCH_ORDER" javaname="PurchaseOrder"/>
    </xb:config>
    

    Notice that you can also map specific element/attribute names to java names.

    /!\ Note: If schema doesn't include targenamespace then use

    <xb:namespace uri="##any">
      <xb:package>org.openuri.easypo.xsdconfig</xb:package>
    </xb:namespace>
    

    to specify package names

    /!\ Note: XmlBeans doesn’t support using two or more sets of java classes (in different packages) mapped to schema types/elements that have the same names and target namespaces, using all in the same class loader. Depending on the direction you are using for the java classes to schema types mapping, some features might not work correctly. This is because even though the package names for the java classes are different, the schema location for the schema metadata (.xsb files) is the same and contains the corresponding implementing java class, so the JVM will always pick up the first on the classpath. This can be avoided if multiple class loaders are used.


    先看一下 scomp 命令: xmlbeans-home/bin 要加入到path 中,

    scomp 參數(shù)圖
    編寫自己的schema文件:weather_latlong.xsd
    <xsd:schema
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:ct
    ="http://beans.jhalo.com"
    targetNamespace
    ="http://beans.jhalo.com"
    elementFormDefault
    ="qualified">
    <!-- This XML Schema describes xml documents
    containing either weather details or latlong
    details of a location based on Zipcode Two Global
    elements Weather and Latlong, and one Global
    Attribute Zipcode are declared.
    -->
     
    <xsd:element name="Weather">
      
    <xsd:complexType>
       
    <xsd:sequence>
        
    <xsd:element name="Temperature"
             type
    ="xsd:float"/>
        
    <xsd:element name="Humidity"
             type
    ="xsd:float"/>
        
    <xsd:element name="Visibility"
             type
    ="xsd:float"/>
        
    <xsd:element name="Datetime"
             type
    ="xsd:dateTime"/>
       
    </xsd:sequence>
      
    <xsd:attribute ref="ct:Zipcode"/>
     
    </xsd:complexType>
     
    </xsd:element>
     
    <xsd:element name="Latlong">
      
    <xsd:complexType>
       
    <xsd:sequence>
        
    <xsd:element name="Latitude"
             type
    ="xsd:string"/>
        
    <xsd:element name="Longitude"
             type
    ="xsd:string"/>
        
    </xsd:sequence>
       
    <xsd:attribute ref="ct:Zipcode"/>
      
    </xsd:complexType>
     
    </xsd:element>
     
    <xsd:attribute name="Zipcode"
             type
    ="xsd:string"/>
    </xsd:schema>


    #


    2
    編寫myconfig.xsdconfig 存放同一目錄


    #
    <xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config"
               xmlns:ct
    ="http://beans.jhalo.com">

      
    <xb:namespace uri="http://beans.jhalo.com http://services.jhalo.com">
        
    <xb:package>org.jhalo</xb:package>
      
    </xb:namespace> 

      
    <xb:namespace uri="##any">
        
    <xb:prefix>Xml</xb:prefix>
        
    <xb:suffix>Bean</xb:suffix>
      
    </xb:namespace>


      
      
    <xb:qname name="ct:Zipcode" javaname="Zip" /> 
      
    <xb:qname name="ct:Datetime" javaname="Today" />


    </xb:config>


    如果不使用myconfig.xsdconfig 進(jìn)行編譯
    scomp -out weather.jar weather_latlong.xsd 
    生成的 weather.jar 為:com/jhalo/beans/xxxx


    使用 .xsdconfig 編譯
    scomp -out weather.jar weather_latlong.xsd  myconfig.xsdconfig
    生成jar 結(jié)構(gòu)為: org/jhalo/

    在寫測試程序發(fā)現(xiàn), Datetime -->Today 也改變了。
    import org.apache.xmlbeans.*;
    import org.jhalo.XmlLatlongDocumentBean;
    import org.jhalo.XmlWeatherDocumentBean;


    XmlWeatherDocumentBean wdoc;
    XmlWeatherDocumentBean.Weather w;
    wdoc 
    = XmlWeatherDocumentBean.Factory.newInstance();
    = wdoc.addNewWeather();
    w.setToday(Calendar.getInstance());

    完畢.:)~


    方向:分布式系統(tǒng)設(shè)計

    posted on 2006-02-14 16:13 java光環(huán) 閱讀(1237) 評論(0)  編輯  收藏 所屬分類: xml


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


    網(wǎng)站導(dǎo)航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 国产成人自产拍免费视频| 亚洲AV综合永久无码精品天堂| 色噜噜狠狠色综合免费视频| 免费无码又爽又刺激高潮的视频 | 亚洲av激情无码专区在线播放 | 国产午夜亚洲精品不卡 | 亚洲国产成人久久综合碰| 欧美日韩亚洲精品| 国产乱人免费视频| 无码的免费不卡毛片视频| 亚洲精品无码不卡在线播HE | 在线免费观看国产视频| 国产AV日韩A∨亚洲AV电影| 婷婷综合缴情亚洲狠狠尤物| 日韩a毛片免费观看| 最新国产AV无码专区亚洲| a级毛片100部免费观看| 中文字幕在线观看亚洲| 好先生在线观看免费播放| 亚洲免费福利在线视频| 免费国产a国产片高清| 色多多www视频在线观看免费| 亚洲色成人中文字幕网站 | baoyu122.永久免费视频| 久久久亚洲精品无码| 无人在线直播免费观看| WWW亚洲色大成网络.COM| 亚洲无码精品浪潮| 国产成人久久AV免费| 亚洲成人激情小说| 亚洲乱码中文字幕综合234| 无码av免费网站| 亚洲色大成网站www尤物| 国产亚洲精品激情都市| 五月婷婷综合免费| 全黄A免费一级毛片| 91精品国产亚洲爽啪在线观看| 色www永久免费视频| 免费成人高清在线视频| 亚洲中文字幕一区精品自拍| 国产亚洲精久久久久久无码77777|