gpt4 book ai didi

xsd - JAXB 选择列表

转载 作者:行者123 更新时间:2023-12-03 17:21:39 25 4
gpt4 key购买 nike

我有以下架构

<complexType name="BookShelf">
<sequence>
<element name="newBook" type="string" minOccurs="0" maxOccurs="unbounded"/>
<element name="oldBook" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>

XJC 生成带有两个列表的 BookShelf 类,一个用于 newBook,一个用于 oldBook。优秀!

现在我希望书籍以任何顺序出现。所以我将我的架构重写为:
<complexType name="BookShelf">
<sequence>
<choice minOccurs="0" maxOccurs="unbounded">
<element name="newBook" type="string"/>
<element name="oldBook" type="string"/>
</choice>
</sequence>
</complexType>

但现在 XJC 生成 BookShelf 时只有一个列表 newBookOrOldBook 类型 List<JAXBElement<String>> .

我不关心书籍出现的顺序,我希望允许 XML 编写器以他\她希望的任何顺序指定书籍,但我仍然希望每种类型的书籍作为生成的 BookShelf 类中的 List 。有什么办法可以实现这一目标吗?

最佳答案

您可以使用 Simplify plugin来自 JAXB2 Basics .它可以简化 @XmlElements@XmlElementRefs属性,如果你真的不关心顺序,这会让事情变得更容易。这是一个示例(摘自文档):

考虑以下选择:

<xs:complexType name="typeWithReferencesProperty">
<xs:choice maxOccurs="unbounded">
<xs:element name="a" type="someType"/>
<xs:element name="b" type="someType"/>
</xs:choice>
</xs:complexType>

这通常会生成一个属性,如:
@XmlElementRefs({
@XmlElementRef(name = "a", type = JAXBElement.class),
@XmlElementRef(name = "b", type = JAXBElement.class)
})
protected List<JAXBElement<SomeType>> aOrB;

您可以使用 simplify:as-element-property element 将此复杂属性改造成两个元素属性或 simplify:as-reference-property作为两个引用属性。

并不是在引用属性的情况下,您必须自定义 xs:element 之一。而不是 xs:choice .
<xs:complexType name="typeWithReferencesProperty">
<xs:choice maxOccurs="unbounded">
<xs:element name="a" type="someType">
<xs:annotation>
<xs:appinfo>
<simplify:as-element-property/>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="b" type="someType"/>
</xs:choice>
</xs:complexType>

结果是:
@XmlElement(name = "a")
protected List<SomeType> a;
@XmlElement(name = "b")
protected List<SomeType> b;

关于xsd - JAXB 选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/888810/

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