1、创建一个Person转换器:
下面我们来创建一个简单的转换器,它能:
a,用来转换Person类
b,将Person实例转换成XML
c,将xml转换为Person实例
(1)首先创建一个PersonConverter类,并让这个类实现Converter接口:
package com.thoughtworks.xstream.examples; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class PersonConverter implements Converter { public boolean canConvert(Class clazz) { return false; } public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { return null; } }
(2) 下面,我们首先告诉转换器,我们只能转换Person类,而不是别的类,包括其子类:(这一步很简单,除非你是用来处理泛型的转换器是会困难一点。)
public boolean canConvert(Class clazz) { return clazz.equals(Person.class); }
(3)编辑转换器
package com.thoughtworks.xstream.examples; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class PersonConverter implements Converter { public boolean canConvert(Class clazz) { return clazz.equals(Person.class); } /** **Marshal方法是用来将对象转换为XML的,他有三个参数: 1,我们准备转换的对象 2,我们准备输出对象的writer 3,当前的marshaling context */ public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { Person person = (Person) value; //创建一个叫做fullname的节点,并将person的名字传给他 //可以任意次数的调用start/end node方法,但需要记住,你必须在打开一个节点之后记住关闭它。 //一般来说,执行转换的操作在setValue方法调用时发生 writer.startNode("fullname"); writer.setValue(person.getName()); writer.endNode(); } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Person person = new Person(); //使用moveDown和moveUp方法在节点树层次中移动,所以,这里我们只需要简单的moveDown,得到值,再moveUp reader.moveDown(); person.setName(reader.getValue()); reader.moveUp(); return person; } }
(4)注册转换器
XStream xStream = new XStream(new DomDriver()); xStream.registerConverter(new PersonConverter());
2、Date转换器示例:
package com.thoughtworks.xstream.examples; import java.text.DateFormat; import java.text.ParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import com.thoughtworks.xstream.converters.ConversionException; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class DateConverter implements Converter { private Locale locale; public DateConverter(Locale locale) { super(); this.locale = locale; } public boolean canConvert(Class clazz) { return Calendar.class.isAssignableFrom(clazz); } public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) { Calendar calendar = (Calendar) value; Date date = calendar.getTime(); DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL, this.locale); writer.setValue(formatter.format(date)); } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { GregorianCalendar calendar = new GregorianCalendar(); //时间本地化 DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL, this.locale); try { calendar.setTime(formatter.parse(reader.getValue())); } catch (ParseException e) { throw new ConversionException(e.getMessage(), e); } return calendar; } } // grabs the current date from the virtual machine Calendar calendar = new GregorianCalendar(); // creates the xstream XStream xStream = new XStream(new DomDriver()); // brazilian portuguese locale xStream.registerConverter(new DateConverter(new Locale("pt", "br"))); // prints the result System.out.println(xStream.toXML(calendar));