JAXB中的XML IDREF返回错误的类? [英] XML IDREF in JAXB returns the wrong class?

查看:79
本文介绍了JAXB中的XML IDREF返回错误的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XSD设计和JAXB还是陌生的.我设法创建了如下的XSD:

I am new to both XSD design and JAXB. I managed to create my XSD as below:

<xsd:element name="policy" >
<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="actor-def" type="tns:actor-def"/>                        
        <xsd:element name="actor-system-def" type="tns:actor-system-def"/>           
        </xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="actor-def">
    <xsd:sequence>
        <xsd:element name="actor-system" type="tns:actor-system-type"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:ID" use="required" />
</xsd:complexType>
<xsd:complexType name="actor-system-type">
    <xsd:attribute name="name" type="xsd:IDREF" use="required" />
</xsd:complexType>

<xsd:complexType name="actor-system-def">
    <xsd:attribute name="name" type="xsd:ID" use="required" />
    <xsd:attribute name="url" type="xsd:anyURI" use="required" />
</xsd:complexType>

基本上,我有一个策略"的根元素,该元素允许定义多个"actor-def"和"actor-system-def".每个"actor-def"将属于一个通过使用ID和IDREF实现的"actor-system-def".

Basically, I am having a root element of "policy" which allows several "actor-def" and "actor-system-def" to be defined. each "actor-def" will belong to one "actor-system-def" that is achieved by using ID and IDREF.

为了与此一起使用JAXB,我使用Eclipse的JAXB Classes for Schema创建工具自动创建了所有必需的类.它会基于XSD文件自动创建以下类:

In order to use JAXB with this, I created all the required classes automatically using Eclipse's JAXB Classes for Schema creation tool. It creates the following classes automatically based on the XSD file:

Policy.java

Policy.java

ActorDef.java

ActorDef.java

ActorSystemDef.java

ActorSystemDef.java

ActorSystemType.java

ActorSystemType.java

除了ActorDef.java文件和其中使用XSD文件中的IDREF定义的actorSystem变量的类型之外,其他所有内容都看起来不错.基本上,ActorDef.java文件具有以下内容:

everything looks fine except for the ActorDef.java file and the type of the actorSystem variable in it which is defined using IDREF in the XSD file. Basically, ActorDef.java file has the following:

@XmlElement(name = "actor-system", required = true)
protected ActorSystemType actorSystem;
public ActorSystemType getActorSystem() {
    return actorSystem;
}

与此有关的问题是,由于从ActorDef.getActorSystem()返回的类型是ActorSystemType类型,因此它是对错误对象的引用.我想要的是对相关ActorSystemDef的引用,以便我可以对此调用getName()方法以获取ActorSystemDef的名称(如下所示)

The problem with this is that since the returned type from the ActorDef.getActorSystem() is of type ActorSystemType, it is a reference to the wrong object. What I want is a reference to the related ActorSystemDef so that I can call the getName() method on that to get the name of the ActorSystemDef (as can be seen below)

JAXBContext myJAXBContext = JAXBContext.newInstance("spec_jaxb");
Unmarshaller myUnmarshaller = myJAXBContext.createUnmarshaller();
Policy myPolicy = (Policy) myUnmarshaller.unmarshal(new  File("src/main/java/spec/private_policy.xml"));

List<ActorDef> actorList = myPolicy.getActorDef();
for (ActorDef actor : actorList) {
    System.out.println(actor.getActorSystem().getName());
}

我能够通过手动修改JAXB工具自动创建的类来使代码正常工作.我将ActorDef.java中actorSystem变量的类型更改为ActorSystemDef,如下所示:

I was able to make the code work by manually modifying the classes that were created automatically by the JAXB tool. I changed the type of the actorSystem variable in the ActorDef.java to ActorSystemDef as below:

protected ActorSystemDef actorSystem;
public ActorSystemDef getActorSystem() {
    return actorSystem;
}

这解决了问题,并且代码可以正常工作,但是我想学习如何纠正XSD文件,以便JAXB类创建工具可以自动纠正上述问题.我认为我在导致此问题的XSD定义中犯了一个错误.我经常修改XSD文件,无法每次都手动进行所有这些更改.

This solved the problem and the code works fine but I want to learn how I can correct my XSD file so that the above problem is automatically corrected by the JAXB class creation tool. I think I am making a mistake in my XSD definition that is causing this. I modify my XSD file very often and can't make all sort of these changes manually every time.

推荐答案

我尝试了您的模式,并得到了ActorDef类的,与您相同:

I tried your schema, and the got the for the ActorDef class, the same as you:

@XmlElement(name = "actor-system", required = true)
protected ActorSystemType actorSystem;

public ActorSystemType getActorSystem() { return actorSystem; }
public void setActorSystem(ActorSystemType value) { this.actorSystem = value; }

但是后来我删除了架构的这一部分(似乎您没有使用它-为什么在那儿?):

But then I removed this part of you schema (as it seems you are not using it - why was it there?):

<xsd:complexType name="actor-system-type">
    <xsd:attribute name="name" type="xsd:IDREF" use="required" />
</xsd:complexType>

,然后将actor-system元素的type从以下位置更改:

And then changed the actor-system element's type from:

<xsd:element name="actor-system" type="tns:actor-system-type"/>

收件人:

<xsd:element name="actor-system" type="tns:actor-system-def"/>

因此,结束模式变为:

<xsd:element name="policy">
<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="actor-def" type="tns:actor-def"/>
        <xsd:element name="actor-system-def" type="tns:actor-system-def"/>
    </xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="actor-def">
    <xsd:sequence>
        <xsd:element name="actor-system" type="tns:actor-system-def"/>
    </xsd:sequence>
    <xsd:attribute name="name" type="xsd:ID" use="required" />
</xsd:complexType>

<xsd:complexType name="actor-system-def">
    <xsd:attribute name="name" type="xsd:ID" use="required" />
    <xsd:attribute name="url" type="xsd:anyURI" use="required" />
</xsd:complexType>

然后使用这个,我生成了类,并为ActorDef得到了:

Then using this one, I generated the classes and got, for ActorDef, this:

@XmlElement(name = "actor-system", required = true)
protected ActorSystemDef actorSystem;

public ActorSystemDef getActorSystem() { return actorSystem; }
public void setActorSystem(ActorSystemDef value) { this.actorSystem = value; }

对我来说,这首先就是您所期望的,对吧?

Seems to me that's what you were expecting in the first place, right?

而且,正如应该的那样,不再生成ActorSystemType.java.

Also, as it should be, the ActorSystemType.java is no longer generated.

这篇关于JAXB中的XML IDREF返回错误的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆