使用jaxb-xjc的xjc和schemagen

jaxb是sun的oxm(object xml mapping)框架api,它包含了
jaxb-api
jaxb-impl
jaxb-xjc
其中jaxb-impl实现了jaxb-api,而xjc是XML Binding Binding Compiler,
可以使用其中的
com.sun.tools.internal.jxc.SchemaGenerator 根据java source 生成schema定义
com.sun.tools.xjc.XJCFacade 则根据schema生成java代码

在maven2项目中

<dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.2.1.1</version>
</dependency>

可以看到依赖关系
jaxb-xjc依赖jaxb-impl
jaxb-impl依赖jaxb-api
jaxb-api依赖stax-api、activation

1、定义xml消息范例
比如你要传输的xml是 sample.xml

<HolidayRequest xmlns="http://mycompany.com/hr/schemas">
    <Holiday>
        <StartDate>2006-07-03</StartDate>
        <EndDate>2006-07-07</EndDate>
    </Holiday>
    <Employee>
        <Number>42</Number>
        <FirstName>Arjen</FirstName>
        <LastName>Poutsma</LastName>
    </Employee>
</HolidayRequest>

2、根据xml消息范例 产生 该范例的schema hr.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:hr="http://mycompany.com/hr/schemas"
        elementFormDefault="qualified"
        targetNamespace="http://mycompany.com/hr/schemas">
    <xs:element name="HolidayRequest">
        <xs:complexType>
            <xs:all>
                <xs:element name="Holiday" type="hr:HolidayType"/>                       
                <xs:element name="Employee" type="hr:EmployeeType"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="HolidayType">
        <xs:sequence>
            <xs:element name="StartDate" type="xs:date"/>
            <xs:element name="EndDate" type="xs:date"/>                                  
        </xs:sequence>                                                                   
    </xs:complexType>
    <xs:complexType name="EmployeeType">
        <xs:sequence>
            <xs:element name="Number" type="xs:integer"/>
            <xs:element name="FirstName" type="xs:string"/>
            <xs:element name="LastName" type="xs:string"/>                               
        </xs:sequence>                                                                   
    </xs:complexType>
</xs:schema>

3、使用 com.sun.tools.xjc.XJCFacade 生成jaxb框架代码
com.sun.tools.xjc.XJCFacade类是个有java main函数入口的工具类
可以在eclipse或者在cmd命令行加入参数来执行具体的任务
可执行 com.sun.tools.xjc.XJCFacade -help 查看具体的调用参数

Usage: xjc [-options ...] ... [-b ] ...
If dir is specified, all schema files in it will be compiled.
If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
Options:
  -nv                :  do not perform strict validation of the input schema(s)
  -extension         :  allow vendor extensions - do not strictly follow the
                        Compatibility Rules and App E.2 from the JAXB Spec
  -b <file/dir>      :  specify external bindings files (each <file> must have its own -b)
                        If a directory is given, **/*.xjb is searched
  -d <dir>           :  generated files will go into this directory
  -p <pkg>           :  specifies the target package
  -httpproxy <proxy> :  set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort
  -httpproxyfile <f> :  Works like -httpproxy but takes the argument in a file to protect password
  -classpath <arg>   :  specify where to find user class files
  -catalog <file>    :  specify catalog files to resolve external entity references
                        support TR9401, XCatalog, and OASIS XML Catalog format.
  -readOnly          :  generated files will be in read-only mode
  -npa               :  suppress generation of package level annotations (**/package-info.java)
  -no-header         :  suppress generation of a file header with timestamp
  -target 2.0        :  behave like XJC 2.0 and generate code that doesnt use any 2.1 features.
  -enableIntrospection :  enable correct generation of Boolean getters/setters to enable Bean Introspection apis
  -contentForWildcard  :  generates content property for types with multiple xs:any derived elements
  -xmlschema         :  treat input as W3C XML Schema (default)
  -relaxng           :  treat input as RELAX NG (experimental,unsupported)
  -relaxng-compact   :  treat input as RELAX NG compact syntax (experimental,unsupported)
  -dtd               :  treat input as XML DTD (experimental,unsupported)
  -wsdl              :  treat input as WSDL and compile schemas inside it (experimental,unsupported)
  -verbose           :  be extra verbose
  -quiet             :  suppress compiler output
  -help              :  display this help message
  -version           :  display version information
 
Extensions:
  -Xinject-code      :  inject specified Java code fragments into the generated code
  -Xlocator          :  enable source location support for generated code
  -Xsync-methods     :  generate accessor methods with the 'synchronized' keyword
  -mark-generated    :  mark the generated code as @javax.annotation.Generated
  -episode <FILE>    :  generate the episode file for separate compilation

其中
-p 指定生成的类的包
-d 生成代码的目录
我们来执行 com.sun.tools.xjc.XJCFacade -p cn.ziki.jaxb.test -d src/main/java hr.xsd
就可以生成带有jaxb注解的java对象还有附带的ObjectFactory.java 和package-info.java类

4、反过来schemagen就是根据带有jaxb注解的类来生成schema文件,类似于hibernate orm的反向生成数据库schema
使用 com.sun.tools.internal.jxc.SchemaGenerator -help 来查看具体的查询参数

Usage: schemagen [-options ...] <java files>
Options:
    -d <path>         :  specify where to place processor and javac generated class files
    -cp <path>        :  specify where to find user specified files
    -classpath <path> :  specify where to find user specified files
    -episode <file>   :  generate episode file for separate compilation
    -version          :  display version information
    -help             :  display this usage message

比如 com.sun.tools.internal.jxc.SchemaGenerator EmployeeType.java HolidayRequest.java HolidayType.java

参考资料
http://www.xyzws.com/scdjws/studyguide/jaxb_samples2.0.html
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/jaxb/xjc.html
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/jaxb/schemagen.html
http://static.springsource.org/spring-ws/site/reference/html/tutorial.html
http://www.w3.org/TR/xmlschema-2/#datatype

原创文章,转载请注明: 转载自海波无痕

本文链接地址: 使用jaxb-xjc的xjc和schemagen

文章的脚注信息由WordPress的wp-posturl插件自动生成

此条目发表在javaee, javascript, server分类目录,贴了, , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论链接可以 移除 nofollow.