XStream XmlPullParserException

狂风中的少年 提交于 2019-12-29 07:41:57

问题


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

    XStream xstream = new XStream();

Is resulting in the following exception:

Exception in thread "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

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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.


回答1:


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.




回答2:


Use

new XStream(new StaxDriver())

xpp and xmlpull are very old codebases

with non-default constructor you could avoid those 2 jars




回答3:


You can too use :

new XSteam(new DomDriver())

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

Output 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 :

<?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>


来源:https://stackoverflow.com/questions/21664249/xstream-xmlpullparserexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!