XMLBeans 多个类型xml文件打包合并

       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

相关文章
相关标签/搜索