Java XStream XmlPullParserException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21664249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:55:57  来源:igfitidea点击:

XStream XmlPullParserException

javaxstream

提问by user3062233

I'm trying to use XStream. I've added the XStream executable Jar file to my project. Executing the following command:

我正在尝试使用 XStream。我已将 XStream 可执行 Jar 文件添加到我的项目中。执行以下命令:

    XStream xstream = new XStream();

Is resulting in the following exception:

导致以下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException

线程“main”中的异常 java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException

at com.thoughtworks.xstream.XStream.<init>(XStream.java:350)
at xstream_test.XmlTrasformer.objectToXml(XmlTrasformer.java:56)
at xstream_test.XmlTrasformer.main(XmlTrasformer.java:31)

Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException

引起:java.lang.ClassNotFoundException:org.xmlpull.v1.XmlPullParserException

at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 3 more

Any idea I might have done wrong? Thanks.

知道我可能做错了吗?谢谢。

回答by A Paul

Make sure you have included all the jars that come with XStream also specially "kxml2.jar" and "xmlpull-1.1.3.1.jar" file. Jar version may deffer incase of yours.

确保您已经包含了 XStream 附带的所有 jar,特别是“kxml2.jar”和“xmlpull-1.1.3.1.jar”文件。Jar 版本可能会延迟以防万一。

回答by Kalpesh Soni

Use

new XStream(new StaxDriver())

xpp and xmlpull are very old codebases

xpp 和 xmlpull 是非常古老的代码库

with non-default constructor you could avoid those 2 jars

使用非默认构造函数,您可以避免使用这两个 jar

回答by Boostaut

You can too use :

你也可以使用:

new XSteam(new DomDriver())

The difference with StaxDriver is the output of convert objet to xml.

与 StaxDriver 的区别在于将对象转换为 xml 的输出。

Output DomDriver :

输出 DomDriver :

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

Output StaxDriver :

输出 StaxDriver :

<?xml version="1.0" ?><person><firstname>Joe</firstname><lastname>Walnes</lastname><phone><code>123</code><number>1234-456</number></phone><fax><code>123</code><number>9999-999</number></fax></person>