Java XStream - 忽略 XML 中不存在的标签
我目前使用如下的一段 XML
<Person>
<Name>Frank Smith</Name>
<Id>100023412</Id>
<DOB>12/05/1954</DOB>
<LasLogin>01/09/2010</LasLogin>
<FavOS>Windows</FavOS> // Wild card that may occasionally appear
</Person>
我遇到的问题是,当使用 XStream 时,我需要能够忽略出现的某些标签(在上面的“FavOS”的情况下) 这些标签将来可能不为人所知或发生变化。有没有办法忽略所有与当前实现不匹配的标签?
(使用XStream 1.3.1)
I currently use a piece of XML like the following
<Person>
<Name>Frank Smith</Name>
<Id>100023412</Id>
<DOB>12/05/1954</DOB>
<LasLogin>01/09/2010</LasLogin>
<FavOS>Windows</FavOS> // Wild card that may occasionally appear
</Person>
What I am stuck with, is when using XStream I need to be able to ignore certain tags that appear (in the case above 'FavOS')
These tags may not be known or change in the future. Is there a way to Ignore all tags that do not match what is currently implemented?
(Using XStream 1.3.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于我花了超过 15 分钟才找到这个答案,所以我想我会把它发布出来。
这似乎会跳过不在您的对象中的 xml 项目。
As it took me more than 15 minutes to find this answer, I thought I would post it.
This seems to skip xml items that are not in your objects.
XStream 1.4.5 支持处理未实现的标签。使用 ignoreUnknownElements 来标记尚未实现或已被删除,并且您正在处理旧的 xml。您还可以指定要忽略的特定标签。
XStream 1.4.5 supports dealing with tags which are not implemented. Use ignoreUnknownElements for tags which are not implemented yet or has been removed and you are dealing with old xml. You can also specify which particular tag you would like to ignore.
首先,感谢您分享这个答案。这非常有用。但是,上面提到的代码有问题。它没有 @Override 注释,这是使用这段代码所必须的。这是有效的更新后的代码:
First of all, thanks for sharing this answer. It was very useful. However, the code mentioned above has issues. It does not have @Override annotations, which are a must to use this piece of code. Here is the updated code that works:
来自 x-stream 常见问题解答:
From the x-stream FAQ:
自 XStream 1.4.5 起,在编组器声明期间使用ignoreEnknownElements() 方法就足够了:
忽略不必要的元素。
Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:
to ignore unnecessary elements.
我问了完全相同的问题。
如何让 XStreamMarshaller 跳过未知绑定?
我收到了一条链接到这篇文章的评论。
我通过扩展 XStreamMarshaller 解决了我的问题。
I asked for exactly the same problem.
How can I make a XStreamMarshaller skip unknown binding?
And I got a comment linking this post.
I solved the my problem by extending the
XStreamMarshaller
.