1

好的。我是 JAXB 的新手,我在使用 XmlID 和 XmlIDREF 时遇到了一些麻烦,其中 ID 位于抽象类中。

这是我的问题:

抽象类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso(EntityImpl.class)
public abstract class AbstractEntity {

    @XmlID
    private String id;

    public AbstractEntity() {
        this.id = generateStringId();
    }


    private static String generateStringId() {
        return "urn:uuid:" + UUID.randomUUID().toString();
    }
}

实现类

@XmlAccessorType(XmlAccessType.FIELD)
public class EntityImpl extends AbstractEntity {

    private String variabel_l = "1";
    private String variabel_2 = "2";
    private String variabel_3 = "3";
    private String variabel_4 = "4";
}

某类

此类包含一个 AbstractEntity,在此示例中为一个字符串。

@XmlAccessorType(XmlAccessType.FIELD)
public class SomeClass {

    @XmlIDREF
    private AbstractEntity ae;
    private String status;

    private SomeClass() {
    }

    public SomeClass(AbstractEntity ae, String status) {
        this.ae= ae;
        this.status = status;
    }

}

此类包含两个列表,一个包含“SomeClass”实例的列表和一个包含“EntityImpl”实例的列表

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Root {

    @XmlElementWrapper
    @XmlElement(name = "result")
    private List<SomeClass> someClassList = new ArrayList<>();


    @XmlElementWrapper
    @XmlElement(name = "entityImpl")
    private List<EntityImpl> entityImplList = new ArrayList<>();

    public void addResult(SomeClass someClass) {
        this.someClassList.add(someClass);
    }

    public void addEntityImpl(EntityImpl EntityImpl) {
        this.entityImplList.add(EntityImpl);
    }

演示代码

impl 被添加到 res 中,并且 res 和 impl 都被添加到它们各自的根目录中。

public static void main(String[] args) throws JAXBException {
    Root root = new Root();
    EntityImpl impl = new EntityImpl();

    SomeClass res = new SomeClass(impl, "something");
    root.addEntityImpl(impl);
    root.addResult(res);


    JAXBContext JAXB_CONTEXT;
    JAXB_CONTEXT = JAXBContext.newInstance(Root.class);

    StringWriter writer = new StringWriter();
    JAXB_CONTEXT.createMarshaller().marshal(root, writer);
    System.out.println(writer.getBuffer().toString());


    }

我想要的结果是对 SomeClass-entity 中 impl 的 Id 的引用,但结果是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <someClassList>
        <result>
            <ae xsi:type="entityImpl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <id>urn:uuid:b8a4c0e4-330d-4ed7-9b68-1517d2eb4c2a</id>
                <variabel_l>1</variabel_l>
                <variabel_2>2</variabel_2>
                <variabel_3>3</variabel_3>
                <variabel_4>4</variabel_4>
                <variabel_5>5</variabel_5>
            </ae>
            <status>something</status>
        </result>
    </someClassList>
    <entityImplList>
        <entityImpl>
            <id>urn:uuid:b8a4c0e4-330d-4ed7-9b68-1517d2eb4c2a</id>
            <variabel_l>1</variabel_l>
            <variabel_2>2</variabel_2>
            <variabel_3>3</variabel_3>
            <variabel_4>4</variabel_4>
            <variabel_5>5</variabel_5>
        </entityImpl>
    </entityImplList>
</root>

如果我将@XmlIDREF 移动到根实体中的 entityImplList,那么引用工作正常,我得到了这个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <someClassList>
        <result>
            <ae xsi:type="entityImpl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <id>urn:uuid:46d0681f-4ec6-49ac-8c22-4954eaa5c680</id>
                <variabel_l>1</variabel_l>
                <variabel_2>2</variabel_2>
                <variabel_3>3</variabel_3>
                <variabel_4>4</variabel_4>
                <variabel_5>5</variabel_5>
            </ae>
            <status>something</status>
        </result>
    </someClassList>
    <entityImplList>
        <entityImpl>urn:uuid:46d0681f-4ec6-49ac-8c22-4954eaa5c680</entityImpl>
    </entityImplList>
</root>

当 REF 位于“SomeClass”中时,是否可以让 XmlID / XmlIDREF 工作,或者这不可能?
有任何想法吗?

4

1 回答 1

1

当我完全按照您的问题运行代码时,我得到以下信息,这不是您得到的输出吗?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <someClassList>
        <result>
            <ae>urn:uuid:b34f3b9e-1086-4dfc-852f-4bc55b5d5a6e</ae>
            <status>something</status>
        </result>
    </someClassList>
    <entityImplList>
        <entityImpl>
            <id>urn:uuid:b34f3b9e-1086-4dfc-852f-4bc55b5d5a6e</id>
            <variabel_l>1</variabel_l>
            <variabel_2>2</variabel_2>
            <variabel_3>3</variabel_3>
            <variabel_4>4</variabel_4>
        </entityImpl>
    </entityImplList>
</root>

更新

根据我和 Ørjan Johansen 的调查,在用于 Windows 的 JDK 1.7.0_21 中包含的 JAXB 的实现中似乎存在与此用例相关的错误。该用例在 Mac 的 JDK 1.7.0_21 和 Windows 的 jdk1.6.0_45 中正常工作。当EclipseLink MOXy用作JAXB (JSR-222)提供程序时,此用例也可以正常工作。

于 2013-05-22T10:25:50.960 回答