gpt4 book ai didi

java - JAXB 将绑定(bind)应用于多个 .xsd 架构

转载 作者:行者123 更新时间:2023-11-30 02:20:34 25 4
gpt4 key购买 nike

我有几个我无法控制的 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>

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

<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>

我怎样才能做到这一点?

最佳答案

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

我已经成功地使架构正常工作,作为引用,您需要做的就是设置 schemaLocation="*" (当然,为了读取所有模式)并用 multiple="true" 标记绑定(bind)元素和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 XJC 接受文件的绑定(bind)(例如 <jxb:class name="Device" /> )。

它基本上可以工作,我无法工作的是 inheritance:extends定制,它不想在与 implements 相同的条件下运行一个...

关于java - JAXB 将绑定(bind)应用于多个 .xsd 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com