gpt4 book ai didi

java - JAXB 抽象类和 XmlID/XmlIDREF

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:05 26 4
gpt4 key购买 nike

好的。我是 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,在本例中包含一个 String。

@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 都被添加到 root 上各自的列表中。

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 实体中 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 工作,或者这是不可能的?
有什么想法吗?

最佳答案

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

<?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)提供商。

关于java - JAXB 抽象类和 XmlID/XmlIDREF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16687792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com