标签:io ar os java sp on art 问题 bs
package com.nexus.XStreamTest; import java.io.Writer; import java.util.ArrayList; import java.util.List; import com.nexus.XStream.Address; import com.nexus.XStream.Person; import com.nexus.XStream.Profile; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.XppDriver; public class Test { /** * @param args */ public static void main(String[] args) { /*User user = new User(); user.setId("id"); user.setName("name"); user.setUrl("url"); xstream.alias("xml", user.getClass()); //在处理对象的方法中添加xstream.autodetectAnnotations(true);这样就可以解决此类问题 // xstream.autodetectAnnotations(true); System.out.println(xstream.toXML(user));*/ 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.alias("PERSON", Person.class); xstream.alias("PROFILE", Profile.class); xstream.alias("ADDRESS", Address.class); System.out.println(xstream.toXML(person)); } /** * 初始化,封装应用 */ private static XStream xstream = new XStream(new XppDriver() { public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { public void startNode(String name, Class clazz) { super.startNode(name); } protected void writeText(QuickWriter writer, String text) { /**自定义格式化**/ writer.write(text); } }; } }); }
package com.nexus.XStreamTest; import com.thoughtworks.xstream.annotations.XStreamOmitField; public class User { //在忽略的字段属性或者方法上添加@XStreamOmitField注解 @XStreamOmitField private String id; private String name; private String url; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
package com.nexus.XStream; 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 + ‘}‘; } }
package com.nexus.XStream; 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 com.nexus.XStream; 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 + ‘\‘‘ + ‘}‘; } }
标签:io ar os java sp on art 问题 bs
原文地址:http://my.oschina.net/u/587105/blog/341470