JAXB 选择列表
我有以下模式
<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 仅包含一个类型为 List
的列表 newBookOrOldBook。
我不关心书籍出现的顺序,我希望允许 XML 编写者按照他/她希望的任何顺序指定书籍,但我仍然希望每种类型的书籍作为生成的 BookShelf 类中的列表。 我有什么办法可以实现这个目标吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Simplify 插件 ://github.com/highsource/jaxb2-basics" rel="nofollow">JAXB2 基础知识。 它可以简化
@XmlElements
和@XmlElementRefs
属性,如果您并不真正关心顺序,这会使事情变得容易得多。 下面是一个示例(摘自文档):考虑以下选择:
这通常会生成如下属性:
您可以使用
simplify:as-element-property
元素将此复杂属性重新建模为两个元素properties 或simplify:as-reference-property
作为两个引用属性。并非如此,在引用属性的情况下,您必须自定义
xs:element
之一,而不是xs:choice
。结果是:
You can use the Simplify plugin from JAXB2 Basics. It can simplify
@XmlElements
and@XmlElementRefs
properties, which makes things a lot easier, if you don't really care about the order. Here's an example (excerpt from the documentation):Consider the following choice:
This will normally generate a property like:
You can use the
simplify:as-element-property
element to remodel this complex property as two element properties orsimplify:as-reference-property
as two reference properties.Not that in the case of a reference property, you have to customize one of the
xs:element
and not thexs:choice
.Results in:
也许是这样的?
当然你可以简化为
Maybe something like this?
Of course you could simplify to
我认为如果不编写一些自定义 java 或 XSLT,这在 JAXB 中是不可能的。
JAXB 不太擅长在具有不同结构(如您的结构)的对象和 xml 之间进行映射。 此外,当转换为 Java 中的两个单独的列表时,XML 中旧书相对于新书的排序将会丢失,并且 JAXB 通常希望保留信息。
以下内容没有回答您的问题,但也许这是朝着您想要的迈出的一步:
我还没有尝试过使用 JAXB,但我认为它会生成一个列表具有两个字段的类,newBook和oldBook。 因此,您不必强制转换或使用instanceof,而只需检查null 即可查看它是哪一个。 正如我所说,这不是一个解决方案,但可能更接近一点。
I don't think this is possible in JAXB, without writing some custom java or XSLT.
JAXB isn't very good at mapping between objects and xml that have different structure, like yours. Also, the ordering of the old book with respect to new books in XML would be lost when converted to two separate lists in Java, and JAXB generally wants to preserve information.
The following does not answer your question, but maybe it's a step towards what you want:
I haven't tried this with JAXB, but I think it will generate a list of a class with two fields, newBook and oldBook. So, you don't have to cast or use instanceof, but can just check for null to see which one it is. As I said, it's not a solution, but maybe a little bit closer.
我认为我必须拒绝在一个元素中混合不同元素的列表(在书本上混合新旧书籍)的想法,特别是因为我计划在其他元素中引用这些元素的列表(新书和旧书列表) 。 如果我不这样做,它很快就会成为 Java 代码中的一场噩梦。 我以以下模式结束:
感谢大家帮助我实现这一点。 这种模式将导致更清晰和简单的 Java 代码以及更易读和可预测的 XML 文档。
I think I have to refuse the idea of mixing lists of different elements in one element (mixing old and new books on a bookself), especially because I'm planning to reference lists of those elements (new and old books lists) in other elements. If I don't it quickly became a nightmare in java code. I ended with the following schema:
Thanks everyone for helping me realize this. This schema will lead to more clear and simple Java code as well as to more readable and predictable XML document.