4

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>

测试版.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,这两个方案之间共享一个元素。我想做的是:

  • 使用 xjc 以两种模式类共享类的方式生成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其视为相同的实体/元素。

4

2 回答 2

6

您可以使用外部绑定文件来指示在类生成期间我们希望将现有类用于名为 Document 的复杂类型。

绑定.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
于 2013-11-07T15:29:14.807 回答
1

如果来自A 的命名空间将等于来自B的命名空间,则xjc必须生成正确的类。person person

于 2013-11-07T13:32:34.783 回答