package test; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringEscapeUtils; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class Start { /** * @param args */ public static void main(String[] args) { System.out .println("----------XStream学 习:http://lavasoft.blog.51cto.com----------"); // 目标对象 Address address1 = new Address("郑州市经三路", "450001"); Address address2 = new Address("西安市雁塔路", "710002"); List<Address> addList = new ArrayList<Address>(); addList.add(address1); addList.add(address2); Profile profile = new Profile("软件工程师", "13512129933", "备注说明"); Person person = new Person("熔岩", "27", profile, addList); // 转换装配 XStream xStream = new XStream(new DomDriver()); /************** 设置类别名 ****************/ xStream.alias("PERSON", Person.class); xStream.alias("PROFILE", Profile.class); xStream.alias("ADDRESS", Address.class); output(1, xStream, person); /************* 设置类成员的别名 ***************/ // 设置Person类的name成员别名Name xStream.aliasField("Name", Person.class, "name"); /* * [注意] 设置Person类的profile成员别名PROFILE,这个别名和Profile类的别名一致, * 这样可以保持XStream对象可以从profile成员生成的xml片段直接转换为Profile成员, * 如果成员profile的别名和Profile的别名不一致,则profile成员生成的xml片段不可 * 直接转换为Profile对象,需要重新创建XStream对象,这岂不给自己找麻烦? */ xStream.aliasField("PROFILE", Person.class, "profile"); xStream.aliasField("ADDLIST", Person.class, "addlist"); xStream.aliasField("Add", Address.class, "add"); xStream.aliasField("Job", Profile.class, "job"); output(2, xStream, person); /******* 设置类成员为xml一个元素上的属性 *******/ xStream.useAttributeFor(Address.class, "zipcode"); /************* 设置属性的别名 ***************/ xStream.aliasAttribute(Address.class, "zipcode", "Zipcode"); output(3, xStream, person); /************* 将xml转为java对象 ******× ****/ String person_xml = "<PERSON>\n" + " <name>熔岩</name>\n" + " <age>27</age>\n" + " <PROFILE>\n" + " <job>软件工程师</job>\n" + " <tel>13512129933</tel>\n" + " <remark>备注说明</remark>\n" + " </PROFILE>\n" + " <addlist>\n" + " <ADDRESS Zipcode=\"450001\">\n" + " <add>郑州市经三路</add>\n" + " </ADDRESS>\n" + " <ADDRESS Zipcode=\"710002\">\n" + " <add>西安市雁塔路</add>\n" + " </ADDRESS>\n" + " </addlist>\n" + "</PERSON>"; String profile_xml = " <PROFILE>\n" + " <job>软件工程师</job>\n" + " <tel>13512129933</tel>\n" + " <remark>备注说明</remark>\n" + " </PROFILE>"; String address_xml = "<ADDRESS Zipcode=\"710002\">\n" + "<add>西安市雁塔路</add>\n" + " </ADDRESS>"; // 同样实用上面的XStream对象xStream // System.out.println(xStream.fromXML(person_xml).toString()); // System.out.println(xStream.fromXML(profile_xml).toString()); System.out.println(xStream.fromXML(address_xml).toString()); } public static void output(int i, XStream xStream, Object obj) { String xml = xStream.toXML(obj); System.out.println(">>>第[ " + i + "]次输出\n"); System.out.println(xml + "\n"); } }
以上是启动类
==========================
package test; import java.util.List; public class Person { private String name; private String age; private Profile profile; private List<Address> addlist; public Person(String name, String age, Profile profile, List<Address> addlist) { this.name = name; this.age = age; this.profile = profile; this.addlist = addlist; } public String toString() { return "Person{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", profile=" + profile + ", addlist=" + addlist + '}'; } }
Person 类,下面两个类是Person的属性
======================
package test; public class Profile { private String job; private String tel; private String remark; public Profile(String job, String tel, String remark) { this.job = job; this.tel = tel; this.remark = remark; } public String toString() { return "Profile{" + "job='" + job + '\'' + ", tel='" + tel + '\'' + ", remark='" + remark + '\'' + '}'; } }
package test; public class Address { private String add; private String zipcode; public Address(String add, String zipcode) { this.add = add; this.zipcode = zipcode; } public String toString() { return "Address{" + "add='" + add + '\'' + ", zipcode='" + zipcode + '\'' + '}'; } }