使用XStream java对象转换成XML遇到的问题

之前看了很多XStream的简单案例,都说的是导入XStream.jar 

然后调试了好久都一直报错!

Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
	at com.thoughtworks.xstream.XStream.(XStream.java:350)
	at com.xstream.Test.main(Test.java:11)
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)


最后终于找到了问题所在,原来是还要导入一个jar包 -------xmlpull.jar


下面是我的一个简单是例子

package com.xstream;

public class Object {
    private String name;
    private String age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
    
}

package com.xstream;

import com.thoughtworks.xstream.XStream;

public class Test {
     public static void main(String[] args){
    	 Object object=new Object();
    	 object.setName("clx");
    	 object.setAge("22");
    	 
    	 XStream x=new XStream();
    	 x.alias("xml", Object.class);
    	 String xml= x.toXML(object);
    	 System.out.println(xml);
     }
}

结果:


  clx
  22




你可能感兴趣的:(使用XStream java对象转换成XML遇到的问题)