XmlBeans 方便的使操做xml 面向了對象,稍微看一下文檔便可知道怎麼使用他.不過官方提供的sample 只是針對單個xml,xsd文件的(好比,讀取,寫入,打包)。經過例子咱們知道若是有一個xml文檔和他的schema,咱們可使用scomp命令打包成一個jar文件,這對於項目中有少數的幾個文件來講還能夠接受。若是一個項目中有十幾甚至幾十個xml文件,難道每一個xml文件都須要打一個jar文件嗎?很顯然,以上的方案是行不通的。
下文講述了做者對於多個xml文件打包合併的經驗.html
舉例說明,項目中有quote.xml文件和quote_list.xml文件,他們分別表明某隻股票信息和某組股票信息.因爲2個xml文件的tag比較多,手動編寫schema 比較浪費時間,可使用如下2個方式生成schemajava
1,使用stylus studio.步驟以下,打開xml文件,xml菜單下 選擇從xml生成架構.具體細節很簡單,不在囉嗦apache
2,使用trang(java寫的xml to schema 的工具),執行 java -jar trang.jar quote.xml quote.xsd,便可在當前文件夾生成quote.xsd架構
自動生成的schema可能不是你想要的,這時候你須要稍加修改,好比類型工具
至此,已經有了xml文件以及他的結構文件schema。spa
接下來,進入本文的重點,打包component
scomp命令爲 comp [opts] [dirs]* [schema.xsd]* [service.wsdl]* [config.xsdconfig]* orm
有不少命令能夠參考幫助,通常打包使用 out參數。假如 全部的xsd 文件都在xsd下 那麼執行server
scomp -out quoteserver-xml.jar xsd/quote.xsd,就能夠完成打包。可是你會發現生成的jar 文件中,包名是系統默認的noNamespace,你可能須要本身的包。這時須要修改xsd文件,在xsd的根節點下加入 targetNamespace,和xmlns 2個節點(2個節點的內容通常須要同樣),這2個節點表示這個xml文件的名字空間,例如xml
quote.xml
http://www.w3.org/2001/XMLSchema" xmlns="http://mi2.finet.com/quoteserver/xml/quote"
targetNamespace="http://mi2.finet.com/quoteserver/xml/quote" elementFormDefault="qualified" attributeFormDefault="unqualified">
http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd" />
一樣對quotes.xml 加入本身的名字空間,以下
http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns="http://mi2.finet.com/quoteserver/xml/quotes" targetNamespace="http://mi2.finet.com/quoteserver/xml/quotes">
http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd" />
能夠看到,quote和quotes 2個xml分別在quoteserver 項目的xml子項目 quote和quotes包中
執行
scomp -out quoteserver-xml.jar xsd/*xsd,生成 quoteserver-xml.jar文件,能夠看到,quote和quotes文件編譯後的類文件分別在
com.finet.mi2.quoteserver.xml.quote和com.finet.mi2.quoteserver.xml.quotes 下.
你必定發現規律了,xmlns="http://mi2.finet.com/quoteserver/xml/quotes",一級域名反過來就是com.finet.mi2,一級域名後邊的順着組合就是他quoteserver.xml.quotes. Xmlbeans 就是按照這樣的規則進行打包的
對與比較熟悉xmlbeans 組件的哥們來講,應該知道xmlbeans打包的時候有個config文件,這個文件也是打包須要用到的,他也能夠規定打包後的包路徑。
以上用到工具下載地址以下:
xmlbeans2.4 http://xmlbeans.apache.org/
trang http://www.thaiopensource.com/download/trang-20030619.zip
Generate XSD from XML blog
http://blog.dotkam.com/2008/05/28/generate-xsd-from-xml/
stylus studio http://www.componentsource.com/products/stylus-studio-xml-enterprise/index.html