当XML中有更多字段时,xStream崩溃

Dan*_*ans 3 java xml xstream xml-serialization

XSTREAM

当我有以下XML代码时:

<xml>
  <version>1.1</version>
  <url>http://www.google.nl</url>
</xml>
Run Code Online (Sandbox Code Playgroud)

我用我的Java代码读到这一切一切正常,但是当XML发生变化时,例如:

<xml>
  <test>test</test>
  <version>1.1</version>
  <url>http://www.google.nl</url>
</xml>
Run Code Online (Sandbox Code Playgroud)

我收到错误,但我希望程序不会停止,并且不要使用现场测试.有没有办法处理这个异常而不停止程序?

Exception in thread "main"         com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldExce    ption: No such field Version.iets
---- Debugging information ----
field               : iets
class               : Version
required-type       : Version
converter-type      :     com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /Version/iets
line number         : 1
version             : null
-------------------------------
Run Code Online (Sandbox Code Playgroud)

Aji*_*mar 5

XStream 1.4.5使您可以轻松处理未知标记.对于尚未实现或已被删除且您正在处理旧xml的标记,请使用ignoreUnknownElements.

http://x-stream.github.io/javadoc/com/thoughtworks/xstream/XStream.html#ignoreUnknownElements%28%29

您还可以指定要忽略的特定标记.