XStreamsignle集合中的几种节点类型
我正在尝试反序列化这样的 XML 文档:
<rootelem>
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
</rootelem>
默认情况下,XStream 只能解析这样的形式:
<rootelem>
<list1>
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType1 arg1="..." />
</list1>
<list2>
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
</list>
</rootelem>
这是因为 XStream 使用下一个格式的集合:
<collection>
<elem .... />
<elem .... />
<elem .... />
</collection>
并且框架标签是强制性的。集合只能包含单一类型的节点。那么如何解析这样的XML文档呢?现在我已经为此编写了自己的转换器,但我想知道还有其他方法吗?
I'm trying to deserialize such XML document:
<rootelem>
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
</rootelem>
By default XStream can parse only such form:
<rootelem>
<list1>
<elementType1 arg1="..." />
<elementType1 arg1="..." />
<elementType1 arg1="..." />
</list1>
<list2>
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
<elementType2 argA="..." argB="..." />
</list>
</rootelem>
This is because XStream use next format for collections:
<collection>
<elem .... />
<elem .... />
<elem .... />
</collection>
and frame tags are obligatory. Collection can contain only single type nodes. So how can I parse such XML document? Now I've written my own convertor for this but I wonder are there some other ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为隐式集合是适合您的解决方案。
http://x-stream.github.io/alias-tutorial.html#implicit
这是示例代码:
结果:
I think that Implicit Collections is the solution for you.
http://x-stream.github.io/alias-tutorial.html#implicit
Here is the sample code:
And the result:
您需要做的第一件事是为所有主要 XML 标记创建 POJO。我假设您给出的示例不是您需要“解组”的实际 XML(这是反序列化的 XML 术语),但为了给您提供一个示例,我将继续使用它;-)。
现在您已经有了 POJO,使用 XStream 将它们编组(序列化)和反编组(反序列化)到 XML 或从 XML 中解编(反序列化)就非常容易了。
此代码现在将在控制台上打印以下内容:
然后,您可以使用 XStreams 多功能“别名”方法来清理名为 XML 元素的令人讨厌的完全限定包,如下所示:
现在将打印出:
Now I know 在您的示例中,您希望所有
实际上都是
标记的属性 ,而不是子标签本身。您可以使用 XStream 强大的 API 来为您完成此操作,无论您喜欢什么。但希望这足以让您开始。顺便说一句,将 XML 转换回 POJO 也同样简单:
根据经验,XStream 是一个“oxmapper”(对象 XML 映射器),它是预先构建的,知道如何将 POJO 转换为有意义的 XML,反之亦然。您始终可以覆盖其默认设置,但更常见的是,我总是发现自己对其默认设置的智能程度感到惊讶。祝你好运。
The first thing you need to do is to create POJOs for all your major XML tags. I assume that the example you gave is not the actual XML you need to "unmarshall" (that's XML lingo for deserialization), but I'll roll with it for the sake of giving you an example to work off of ;-).
Now that you have your POJOs, using XStream to marshall (serialize) and unmarshall (deserialize) them to and from XML is quite easy.
This code will now print the following to the console:
You can then clean up the nasty, fully-qualified package named XML elements by using XStreams versatile "alias" methods, like so:
Which will now print out:
Now I know that in your example, you wanted all the
<arg>
s to actually be attributes of the<elementType>
tags, not child tags themselves. You can use XStream's powerful API to do this for you, however you like. But hopefully this is enough to get you started.And by the way, turning your XML back into a POJO is just as easy:
As a rule of thumb, XStream is an "oxmapper" (Object-XML Mapper), and it come pre-built to know how to turn your POJO into meaningful XML and vice versa. You can always override its defaults, but more often then not I have always found myself surprised with how smart its defaults are. Best of luck to you.