所以我试图使用XStream库将一个简单的对象序列化为XML文件。
这些导致以下异常抱怨没有找到类Person。我已经尝试将XStream注释添加到Person类,但这也没有成功。
使用XStream库
(de)序列化对象的正确方法是什么Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:350)
at XMLTest.main(XMLTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
为了完整性,我在下面添加我的代码。
import com.thoughtworks.xstream.XStream;
public class XMLTest {
public static void main(String[] args) {
Person john = new Person("John", "Doe", 1234);
XStream xstream = new XStream();
String xml = xstream.toXML(john);
Person newJohn = (Person) xstream.fromXML(xml);
System.out.println("Hi " + newJohn.getFirstname());
}
}
和
public class Person {
private String firstname;
private String lastname;
private int age;
public Person(String firstname, String lastname, int age) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
答案 0 :(得分:0)
使用
XStream xstream = new XStream(new StaxDriver());
代替。
解释是,如果你这样做
XStream xstream = new XStream(new StaxDriver());
然后你需要在类路径中使用xstream- [version] .jar,xpp3- [version] .jar和xmlpull- [version] .jar。“但是XStream xstream = new XStream(new StaxDriver());
不需要从Java 6开始的XPP3库< / p>
一个非常简单的解释,可以在repo-source
中获得更多信息