XStream的使用方法

来源:互联网 发布:电动机绕组数据技术 编辑:程序博客网 时间:2024/05/14 21:02
import com.thoughtworks.xstream.XStream;class Date {    int year = 2004;    int month = 8;    int day = 15;}public class Deserialize {    public static void main(String[] args) {        XStream xstream = new XStream();        Date date = new Date();        xstream.alias("date", Date.class);        String xml = xstream.toXML(date);        System.out.print(xml);        Date newdate = (Date)xstream.fromXML(xml);        newdate.month = 12;        newdate.day = 2;        String newxml = xstream.toXML(newdate);        System.out.print("\n\n" + newxml);    }}

这里的输出结果如下

<date>  <year>2004<year>  <month>8<month>  <day>15<day><date><date>  <year>2004<year>  <month>12<month>  <day>2<day><date>
细心的人可能注意到了,输出的结果与类定义的属性是一致的,如果你属性是小写的在java类中,

想要把大写的属性在XML中的值映射到java类中,那就要用到xstream.aliasField的方法了。