JAXB将绑定应用于多个.xsd架构

时间:2017-10-27 11:19:38

标签: java jaxb jaxb2 xjc jaxb2-basics

我有几个我无法控制的XML模式,我想在通过XJC生成Java类时应用某些自定义。

某些自定义应适用于所有.xsd个文件,但我不确定如何实现此目的。

目前我有.xjb个文件:

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="../schemas/A.xsd">
         <jxb:schemaBindings>
            <jxb:package name="my.foo.a" />
        </jxb:schemaBindings> 

        <jxb:bindings node="//xs:complexType[@name='A']">
            <jxb:class name="A" />
            <inheritance:implements>my.foo.interfaces.IA</inheritance:implements>           
         </jxb:bindings>

         <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
         </jxb:bindings>             
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/B.xsd">        
        <jxb:schemaBindings>
            <jxb:package name="my.foo.b" />
        </jxb:schemaBindings>

        <jxb:bindings node="//xs:complexType[@name='B']">
            <jxb:class name="B" />
            <inheritance:implements>my.foo.interfaces.IB</inheritance:implements>       
         </jxb:bindings>

        <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
         </jxb:bindings>             
    </jxb:bindings>      
</jxb:bindings>

这个Ant任务:

<taskdef name="xjc" classname="org.jvnet.jaxb2_commons.xjc.XJC2Task">
    <classpath>
        <fileset dir="../../dependencies/lib" >
            <include name="activation-*.jar"/>
            <include name="jaxb-api-*.jar"/>
            <include name="jaxb-impl-*.jar"/>
            <include name="jsr173_api-*.jar"/>
            <include name="stax-api-*.jar"/>

            <include name="jaxb2-basics-jaxb-xjc-*.jar"/>
            <include name="jaxb-xjc-*.jar"/>
            <include name="jaxb2-basics-ant-*.jar"/>
        </fileset>
    </classpath>
</taskdef>

<xjc destdir="../src" extension="true" header="false" binding="bindings.xjb">
    <arg line="-Xinheritance"/>

    <schema dir="../schemas">
        <include name="*.xsd"/>
        <exclude name="Foo.xsd"/>
    </schema>

    <!-- Plugins -->
    <classpath>
        <fileset dir="../../dependencies/lib">
            <!-- JAXB2 Basics library -->
            <include name="jaxb2-basics-*.jar"/>
            <!-- JAXB2 Basics library dependencies -->
            <include name="jaxb2-basics-runtime-*.jar"/>
            <include name="jaxb2-basics-tools-*.jar"/>
            <include name="javaparser-*.jar"/>
            <include name="commons-beanutils-*.jar"/>
            <include name="commons-lang-*.jar"/>
            <include name="commons-logging-*.jar"/>
        </fileset>
    </classpath>
</xjc>

最后我想做的是以某种方式指定公共块(我有多个),即:

<jxb:bindings node="//xs:complexType[@name='Device']">
    <jxb:class name="Device" />
    <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
</jxb:bindings> 

对于我的所有.xsd个文件。

假设,这样的事情:

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="../schemas/*.xsd">
        <jxb:bindings node="//xs:complexType[@name='Device']">
            <jxb:class name="Device" />
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
        </jxb:bindings>  
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/A.xsd">
        <jxb:schemaBindings>
            <jxb:package name="my.foo.a" />
        </jxb:schemaBindings> 

        <jxb:bindings node="//xs:complexType[@name='A']">
            <jxb:class name="A" />
            <inheritance:implements>my.foo.interfaces.IA</inheritance:implements>           
        </jxb:bindings>           
    </jxb:bindings>

    <jxb:bindings schemaLocation="../schemas/B.xsd">        
        <jxb:schemaBindings>
            <jxb:package name="my.foo.b" />
        </jxb:schemaBindings>

        <jxb:bindings node="//xs:complexType[@name='B']">
            <jxb:class name="B" />
            <inheritance:implements>my.foo.interfaces.IB</inheritance:implements>       
        </jxb:bindings>       
    </jxb:bindings>      
</jxb:bindings>

我该如何做到这一点?

1 个答案:

答案 0 :(得分:1)

所以,对于(大部分)在这里使用这个答案:https://stackoverflow.com/a/18592030/770023

我设法让架构正常工作,作为参考你需要做的就是设置schemaLocation="*"(当然是为了读取所有的模式)并用multiple="true"标记绑定元素required="false"因为某些模式可能包含这些元素(一次或多次),有些可能根本不包含它们。

<jxb:bindings version="2.1" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
  jxb:extensionBindingPrefixes="inheritance">

    <jxb:bindings schemaLocation="*">
        <jxb:bindings node="//xs:complexType[@name='Device']" multiple="true" required="false">
            <inheritance:implements>my.foo.interfaces.IDevice</inheritance:implements>          
        </jxb:bindings>  
    </jxb:bindings>
    <!-- rest of the bindings (per schema ones) go here -->
</jxb:bindings>

请注意,对于某些(我不知道)的原因,我必须删除class绑定(例如。<jxb:class name="Device" />),以便XJC接受该文件。

我主要工作,我无法工作的是inheritance:extends自定义,它不想在与implements条件相同的条件下运行。 ..