在 JAXB 中使用 xsd 计划中的 xjc 自动生成类时。
alpha.xsd
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="alpha">
<xs:complexType>
<xs:sequence>
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
beta.xml
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
<xs:complexType>
<xs:sequence>
<xs:element name="class">
<xs:complexType>
<xs:sequence>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
正如您所看到的,这两个方案之间共享Person
个元素。我想做的是:
ObjectFactory
类(输出类将在一个包中)localScoping="toplevel"
)Person
类与/alpha/persons/person
绑定/country/class/person
,因此没有创建两个Person类这样做的目的是解组一个xml,应用业务逻辑并创建另一个作为输出,其中一些元素(如Person
)相同并为两个xml文件共享。两个文件的命名空间都是相同的。
如果您能向我提供完整的.xjb绑定设置文件,我将不胜感激。到目前为止我的只包含:
<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:extensionBindingPrefixes="xjc">
<jxb:globalBindings localScoping="toplevel"/>
</jxb:bindings>
当然我得到名称冲突错误,因为我不知道如何设置绑定编译器以将Person
视为同一个实体/元素。
答案 0 :(得分:4)
您可以使用外部绑定文件来指示在类生成期间,我们希望将现有类用于名为Document的复杂类型。
<强> binding.xml 强>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="beta.xsd">
<jxb:bindings node="//xs:element[@name='person']/complexType">
<jxb:class ref="alpha.Person"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC致电
xjc -b binding.xml beta.xsd
答案 1 :(得分:0)
如果来自 A 的person
的名称空间将等于来自 B的名称空间 person
, xjc 必须生成正确的类。