java – 在XML模式中指定IDREF的类型

前端之家收集整理的这篇文章主要介绍了java – 在XML模式中指定IDREF的类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用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>

我可以像这样定义玩家:

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

但是在Java代码中,当我尝试检索一个播放器时,它将作为类型对象返回,我必须将其转换为一个人.在这一点上,如果有人错误地引用了一个Team对象,那么我有错误可以在验证时被捕获.我想指定这样的东西:

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

但是据我所知,没有办法指定一个类型,就像我在这里所做的那样设计的属性“reftype”一样.可以使用IDREF来完成吗?如果没有,还有另一种方法吗?

解决方法

您可以简单地将baseType绑定应用于播放器元素.就像是:
<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/

猜你在找的Java相关文章