12

我正在使用 xjc 从 XML 模式生成 Java 对象。我想使用 IDREF 在文档中多次引用同一个元素。我还想将 IDREF 引用的对象限制为特定类型。我想这样做是为了模式验证,但也是为了在 Java 代码中,引用的对象作为特定类型而不是 Object 类型返回。例如,假设我想要一个模式来描述以下内容:

<teams>
  <team id="team1">
    <coach>coachz</coach>
    <player>homestar</player>
    <player>marzipan</player>
    <player>strongsad</player>
    <player>strongbad</player>
  </team>

  <team id="team2">
    <coach>bubs</coach>
    <player>homesar</player>
    <player>thecheat</player>
    <player>poopsmith</player>
    <player>bubs</player>
  </team>

  <team id="allstars">
    <coach>poopsmith</coach>
    <player>coachz</player>
    <player>bubs</player>
    <player>kingoftown</player>
    <player>strongbad</player>
  </team>
</teams>

<people>
 <person id="coachz">Coach Z</person>
 <person id="homesar">Homesar</person>
 <person id="homestar">Homestar</person>
 <person id="strongbad">Strong Bad</person>
 <person id="strongsad">Strong Sad</person>
 <person id="marzipan">Marzipan</person>
 <person id="bubs">Bubs</person>
 <person id="kingoftown">King of Town</person>
 <person id="poopsmith">The Poopsmith</person>
 <person id="thecheat">The Cheat</person>
</people>

我可以player这样定义:

<xs:element name="player" type="xs:IDREF" maxOccurs="unbounded"/>

但是在 Java 代码中,当我尝试检索一个播放器时,它将作为类型对象返回,我必须将它转换为一个人。那时,如果有人错误地引用了 Team 对象,我就有可能在验证时发现的错误要处理。我想指定这样的东西:

<xs:element name="player" type="xs:IDREF"reftype="person"maxOccurs="unbounded" />

但据我所知,没有办法像我在此处使用人为的属性“reftype”所做的那样指定类型。这可以使用 IDREF 完成吗?如果没有,还有其他方法吗?

4

2 回答 2

13

您可以简单地将baseType绑定应用于您的player元素。就像是:

<jaxb:bindings node="xsd:element[@name='player']">
    <jaxb:property>
        <jaxb:baseType name="....Person"/>
    </jaxb:property>
</jaxb:bindings>

您可能需要为您的架构找出正确的绑定位置。

我的代码示例:

架构:

<xsd:complexType name="HJIII-53-A">
    <xsd:sequence>
        <xsd:element name="b" type="xsd:IDREF"/>
        <xsd:element name="b1" type="test:HJIII-53-B"/>
        <xsd:element name="c" type="xsd:IDREFS"/>
        <xsd:element name="c1" type="test:HJIII-53-C" minOccurs="0" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>

绑定:

<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
    <jaxb:globalBindings localScoping="toplevel">
        <jaxb:serializable/>
    </jaxb:globalBindings>
    <jaxb:bindings node="xsd:complexType[@name='HJIII-53-A']//xsd:element[@name='b']">
        <jaxb:property>
            <jaxb:baseType name="org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53B"/>
        </jaxb:property>
    </jaxb:bindings>
    <jaxb:bindings node="xsd:complexType[@name='HJIII-53-A']//xsd:element[@name='c']">
        <jaxb:property>
            <jaxb:baseType name="org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53C"/>
        </jaxb:property>
    </jaxb:bindings>
</jaxb:bindings>

生成的代码:

@XmlElement(required = true, type = Object.class)
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53B b;
@XmlElement(required = true)
protected org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53B b1;
@XmlList
@XmlElement(required = true, type = Object.class)
@XmlIDREF
protected List<org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53C> c;
protected List<org.jvnet.hyperjaxb3.ejb.tests.issuesjpa2.HJIII53C> c1;

见:https ://svn.java.net/svn/hj3~svn/trunk/ejb/tests/issues-jpa2/src/main/resources/

于 2013-04-29T22:28:57.873 回答
11

lexicore 的回答给了我我需要的东西(我建议投票赞成他的回答而不是我的回答)。但是,我使用的是内联注释而不是单独的绑定文件。使用我的 Homestar 示例,这就是内联注释的样子:

<xs:element name="player" type="xs:IDREF" maxoccurs="unbounded">
  <xs:annotation>
    <xs:appinfo>
      <jaxb:property>
        <jaxb:baseType name="Person"/>
      </jaxb:property>
    </xs:appinfo>
  </xs:annotation>
</xs:element>
于 2013-04-29T23:20:51.690 回答