标签:soft mars next 详细信息 iso rsh val ... 序列
class Person//JavaBean实体类 { private String name; private int age; public Person(String name,int age) { this.name=name; this.age=age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(); //XML序列化 String xml = xstream.toXML(bean); System.out.println(xml); //XML反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); //Json序列化 String json=xstream.toXML(bean); System.out.println(json); //Json反序列 bean=(Person)xstream.fromXML(json); System.out.println(bean); } }
程序运行结果:
<test.Person> <name>张三</name> <age>19</age> </test.Person> Person [name=张三, age=19] {"test.Person":{"name":"张三","age":19}} Person [name=张三, age=19
实例2:
package com.xstream.xml; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.thoughtworks.xstream.XStream; public class Test { public static void main(String[] args) { Person p = new Person("小名", 13); Person p1= new Person("小红", 12); Person p2 = new Person("小花", 11); List<Person> list = new ArrayList(); list.add(p); list.add(p1); list.add(p2); Persons per = new Persons(list); XStream xs = new XStream(); xs.addImplicitCollection(Persons.class, "list");//省略集合根节点 xs.alias("person", Person.class);//为类名节点重命名 xs.alias("persons", Persons.class); //序列化为xml String xml = xs.toXML(per); System.out.println("序列化结果:"); System.out.println(xml); //反序列化为对象 System.out.println("反序列化结果:"); per= (Persons) xs.fromXML(xml); System.out.println(per.getList()); } }
package com.xstream.xml; public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
package com.xstream.xml; import java.util.List; public class Persons { private List list; public Persons(List<Person> list) { this.list = list; } public List getList() { return list; } public void setList(List list) { this.list = list; } }
程序运行结果: 序列化结果: <persons> <person> <name>小名</name> <age>13</age> </person> <person> <name>小红</name> <age>12</age> </person> <person> <name>小花</name> <age>11</age> </person> </persons> 反序列化结果: [Person [name=小名, age=13], Person [name=小红, age=12], Person [name=小花, age=11]]
public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); //XStream xstream = new XStream();//需要XPP3库 //XStream xstream = new XStream(new DomDriver());//不需要XPP3库 XStream xstream = new XStream(new StaxDriver());//不需要XPP3库开始使用Java6 xstream.alias("人",Person.class);//为类名节点重命名 //XML序列化 String xml = xstream.toXML(bean); System.out.println(xml); //XML反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } }
程序运行结果:
<人><name>张三</name><age>19</age></人> Person [name=张三, age=19]
public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器 xstream.setMode(XStream.NO_REFERENCES);//设置reference模型,不引用 xstream.alias("人",Person.class);//为类名节点重命名 //Json序列化 String xml = xstream.toXML(bean); System.out.println(xml); //Json反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 结果: {"人":{"name":"张三","age":19}} Person [name=张三, age=19]
public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(); xstream.aliasPackage("com.lzw", "test");//为包名称重命名 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <com.lzw.Person> <name>张三</name> <age>19</age> </com.lzw.Person> Person [name=张三, age=19]
(2)为类重命名:Xstream.alias()方法
public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(); xstream.alias("人", Person.class);//为类名节点重命名 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <人> <name>张三</name> <age>19</age> </人> Person [name=张三, age=19]
(3)为字段重命名:Xstream.aliasField()方法
public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(); xstream.aliasField("姓名", Person.class,"name");//为类的字段节点重命名 xstream.aliasField("年龄", Person.class,"age");//为类的字段节点重命名 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <test.Person> <姓名>张三</姓名> <年龄>19</年龄> </test.Person> Person [name=张三, age=19]
(4)省略集合根节点:Xstream.addImplicitCollection()方法
class Person { private String name; private int age; private List friends; public Person(String name, int age, String... friends) { this.name = name; this.age = age; this.friends = Arrays.asList(friends); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]"; } } public class Test { public static void main(String[] args) { Person bean =new Person("张三",19,"李四","王五","赵六"); XStream xstream = new XStream(); xstream.addImplicitCollection(Person.class, "friends");//省略集合根节点 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <test.Person> <name>张三</name> <age>19</age> <string>李四</string> <string>王五</string> <string>赵六</string> </test.Person> Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(5)把字段节点设置成属性:Xstream.useAttributeFor()方法
public class Test { public static void main(String[] args) { Person bean =new Person("张三",19,"李四","王五","赵六"); XStream xstream = new XStream(); xstream.useAttributeFor(Person.class, "name");//把字段节点设置成属性 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <test.Person name="张三"> <age>19</age> <friends class="java.util.Arrays$ArrayList"> <a class="string-array"> <string>李四</string> <string>王五</string> <string>赵六</string> </a> </friends> </test.Person> Person [name=张三, age=19, friends=[李四, 王五, 赵六]
(6)隐藏字段:xstream.omitField()方法
public class Test { public static void main(String[] args) { Person bean =new Person("张三",19,"李四","王五","赵六"); XStream xstream = new XStream(); xstream.omitField(Person.class, "friends");//把字段节点隐藏 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <test.Person> <name>张三</name> <age>19</age> </test.Person> Person [name=张三, age=19, friends=null]
XStream xstream = new XStream(); xstream.processAnnotations(Person.class);//应用Person类的注解 xstream.autodetectAnnotations(true);//自动检测注解
(2)重命名注解:@XStreamAlias()
@XStreamAlias("人") class Person { @XStreamAlias("姓名") private String name; @XStreamAlias("年龄") private int age; @XStreamAlias("朋友") private List friends; public Person(String name, int age, String... friends) { this.name = name; this.age = age; this.friends = Arrays.asList(friends); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]"; } } 程序运行结果: <人> <姓名>张三</姓名> <年龄>19</年龄> <朋友 class="java.util.Arrays$ArrayList"> <a class="string-array"> <string>李四</string> <string>王五</string> <string>赵六</string> </a> </朋友> </人> Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(3)省略集合根节点:@XStreamImplicit
class Person { private String name; private int age; //@XStreamImplicit//只隐藏集合根节点 @XStreamImplicit(itemFieldName="朋友")//设置重复的节点名,可能会导致无法反序列化 private List<String> friends; public Person(String name, int age, String... friends) { this.name = name; this.age = age; this.friends = Arrays.asList(friends); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]"; } } 程序运行结果: <test.Person> <name>张三</name> <age>19</age> <朋友>李四</朋友> <朋友>王五</朋友> <朋友>赵六</朋友> </test.Person> Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(4)把字段节点设置成属性:@XStreamAsAttribute
class Person { @XStreamAsAttribute private String name; @XStreamAsAttribute private int age; private List<String> friends; public Person(String name, int age, String... friends) { this.name = name; this.age = age; this.friends = Arrays.asList(friends); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]"; } } 程序运行结果: <test.Person name="张三" age="19"> <friends class="java.util.Arrays$ArrayList"> <a class="string-array"> <string>李四</string> <string>王五</string> <string>赵六</string> </a> </friends> </test.Person> Person [name=张三, age=19, friends=[李四, 王五, 赵六]]
(5)隐藏字段:@XStreamOmitField
class Person { private String name; private int age; @XStreamOmitField private List<String> friends; public Person(String name, int age, String... friends) { this.name = name; this.age = age; this.friends = Arrays.asList(friends); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", friends=" + friends + "]"; } } 程序运行结果: <test.Person> <name>张三</name> <age>19</age> </test.Person> Person [name=张三, age=19, friends=null]
(6)设置转换器:@XStreamConverter()
class Person { private String name; private int age; @XStreamConverter(value=BooleanConverter.class,booleans={false},strings={"男","女"}) private boolean sex; public Person(String name, int age, boolean sex) { this.name = name; this.age = age; this.sex=sex; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } } 程序运行结果: <test.Person> <name>张三</name> <age>19</age> <sex>男</sex> </test.Person> Person [name=张三, age=19, sex=true]
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class PersonConverter implements Converter { @Override//定义转换器能转换的JavaBean类型 public boolean canConvert(Class type) { return type.equals(Person.class); } @Override//把对象序列化成XML或Json public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { Person person = (Person) value; writer.startNode("姓名"); writer.setValue(person.getName()); writer.endNode(); writer.startNode("年龄"); writer.setValue(person.getAge()+""); writer.endNode(); writer.startNode("转换器"); writer.setValue("自定义的转换器"); writer.endNode(); } @Override//把XML或Json反序列化成对象 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Person person = new Person("",-1); reader.moveDown(); person.setName(reader.getValue()); reader.moveUp(); reader.moveDown(); person.setAge(Integer.parseInt(reader.getValue())); reader.moveUp(); return person; } } public class Test { public static void main(String[] args) { Person bean =new Person("张三",19); XStream xstream = new XStream(); xstream.registerConverter(new PersonConverter());//注册转换器 //序列化 String xml = xstream.toXML(bean); System.out.println(xml); //反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: <test.Person> <姓名>张三</姓名> <年龄>19</年龄> <转换器>自定义的转换器</转换器> </test.Person> Person [name=张三, age=19]
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } } public class Test { public static void main(String[] args) throws IOException { XStream xstream = new XStream(); ObjectOutputStream out = xstream.createObjectOutputStream(System.out); out.writeObject(new Person("张三",12)); out.writeObject(new Person("李四",19)); out.writeObject("Hello"); out.writeInt(12345); out.close(); } } 程序运行结果: <object-stream> <test.Person> <name>张三</name> <age>12</age> </test.Person> <test.Person> <name>李四</name> <age>19</age> </test.Person> <string>Hello</string> <int>12345</int> </object-stream>
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { String s="<object-stream><test.Person><name>张三</name><age>12</age></test.Person><int>12345</int></object-stream>"; StringReader reader = new StringReader(s); XStream xstream = new XStream(); ObjectInputStream in = xstream.createObjectInputStream(reader); System.out.println((Person) in.readObject()); System.out.println(in.readInt()); } } 程序运行结果: Person [name=张三, age=12] 12345
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp")); List list = new XmlArrayList(strategy); list.add(new Person("张三",13));//保存数据 list.add(new Person("李四",21)); list.add(new Person("王五",17)); } }
public class Test { public static void main(String[] args) throws IOException, ClassNotFoundException { PersistenceStrategy strategy = new FilePersistenceStrategy(new File("D:\\tmp")); List list = new XmlArrayList(strategy); for (Iterator it = list.iterator(); it.hasNext();) { System.out.println((Person) it.next()); it.remove();//删除对象序列化文件 } } } 程序运行结果: Person [name=张三, age=13] Person [name=李四, age=21] Person [name=王五, age=17]
@XStreamAlias("人") class Person { @XStreamAlias("姓名") private String name; @XStreamAlias("年龄") private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class Test { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(new JettisonMappedXmlDriver());//设置Json解析器 xstream.autodetectAnnotations(true); //Json序列化 String xml = xstream.toXML(bean); System.out.println(xml); //Json反序列化 bean=(Person)xstream.fromXML(xml); System.out.println(bean); } } 程序运行结果: {"人":{"姓名":"张三","年龄":19}} Person [name=张三, age=19]
class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } } public class Test00 { public static void main(String[] args) { Person bean=new Person("张三",19); XStream xstream = new XStream(new JsonHierarchicalStreamDriver() { public HierarchicalStreamWriter createWriter(Writer writer) { return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE); } }); //Json序列化 String xml = xstream.toXML(bean); System.out.println(xml); } } 程序运行结果: { "name": "张三", "age": 19 }
本文来源: http://www.cnblogs.com/LiZhiW/p/4313493.html
标签:soft mars next 详细信息 iso rsh val ... 序列
原文地址:http://www.cnblogs.com/SEVEN-wkq/p/6871927.html