首页 > 代码库 > Xstream序列化对象为XML
Xstream序列化对象为XML
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 + ‘\‘‘ + ‘}‘; } }
Xstream序列化对象为XML
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。