不同XML /根目录下JAXB中子元素的共享类

dev*_*ull 4 java xml jaxb

JAXB当通过使用自动类生成XJCXSD方案。

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

如您所见Person,这两个方案之间共享一个元素。我想做的是:

  • 使用xjc生成类ObjectFactory,这两种模式类都共享类(输出类将在一个包中)
  • 不使用嵌套的静态类(带有属性localScoping="toplevel"
  • 使用Person类绑定/alpha/persons/person/country/class/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>
Run Code Online (Sandbox Code Playgroud)

当然,我遇到名称冲突错误,因为我不知道如何将绑定编译器设置Person为相同的实体/元素。

bdo*_*han 5

您可以使用外部绑定文件来指示在类生成期间,我们希望将现有类用于称为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>
Run Code Online (Sandbox Code Playgroud)

XJC电话

xjc -b binding.xml beta.xsd
Run Code Online (Sandbox Code Playgroud)