我正在使用 JAXB 和 intellij 网络服务插件从 XSD 创建 java 文件。我有两个定义相同对象的 XSD,但是当我使用“从 XML 模式生成 Java 代码”创建它们时,该对象使用他自己的包创建了两次。我已经尝试使用 import xsd 并使用 ref 属性,我得到了相同的结果。
这是一个例子:
这是第一个XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.msp-gs.com/workflow"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wc="http://www.example.com/workflow"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings enableJavaNamingConventions="true">
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="WC">
<xs:complexType>
<xs:sequence>
<xs:element name="Example"
type="wc:Restriction"
minOccurs="1"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Restriction">
<xs:attribute type="xs:string"
name="authorizationTreeId"/>
</xs:complexType>
</xs:schema>
这是第二个XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.msp-gs.com/workflow"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:fd="http://www.example.com/workflow"
attributeFormDefault="unqualified"
elementFormDefault="qualified"
jaxb:version="1.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings enableJavaNamingConventions="true">
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="FD">
<xs:complexType>
<xs:sequence>
<xs:element name="Example"
type="fd:Restriction"
minOccurs="1"
maxOccurs="unbounded">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Restriction">
<xs:attribute type="xs:string"
name="authorizationTreeId"/>
</xs:complexType>
</xs:schema>
我希望 Restriction 是同一个对象。
谢谢。
最佳答案
您可以告诉 JAXB 使用现有的 Java 类,而不是使用外部绑定(bind)文件生成一个类,如下所示。在下面的示例中,我们告诉 JAXB 使用现有的 Product
类:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bindings version="2.1" xmlns="http://java.sun.com/xml/ns/jaxb">
<bindings scd="x-schema::tns"
xmlns:tns="http://www.example.org/Product">
<schemaBindings map="false"/>
<bindings scd="tns:product">
<class ref="org.example.product.Product"/>
</bindings>
</bindings>
</bindings>
如果您使用 XJC 工具从 XML 模式生成类,您可以使用 -episode
标志让 XJC 生成指向它生成的所有类的绑定(bind)文件。这将允许您重用以前生成的类。
xjc -d out -episode product.episode Product.xsd
了解更多信息
关于xsd - 如何使两个 XSD 使用相同的自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701141/