java对象是可以直接与xml相互转换的,但需要导入包:xstream-1.3.1.jar,在创建对象的时候,注意一定要new一个DomDriver;
成功的测试代码如下:
package com.xml; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; import com.user.User; public class TestXml { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub XStream stream = new XStream(new DomDriver()); User user = new User("sdkf", "14"); String users = stream.toXML(user); System.out.println(users); User user1 = (User)stream.fromXML(users); System.out.println(user1.getAge()); } }