Xstream施用示例【将对象序列化为XML和将XML反序列为对象】
Xstream使用示例【将对象序列化为XML和将XML反序列为对象】Xstream资源下载地址:http://xstream.codehaus.or
Xstream使用示例【将对象序列化为XML和将XML反序列为对象】
Xstream资源下载地址:http://xstream.codehaus.org/download.html
必须包:xstream-1.3.1.jar
测试程序
Person.java
?
Java代码??
- <span?style="font-size:?large;">package?com.xstream.test;??
- ??
- public?class?Person?{??
- ????private?String?firstName;??
- ?????private?String?lastName;??
- ?????private?PhoneNumber?phonex;??
- ?????private?PhoneNumber?fax;??
- ?????public?Person(String?firstName,String?lastName){??
- ?????????this.firstName=firstName;??
- ?????????this.lastName=lastName;??
- ?????}??
- ???????
- ????public?String?getFirstName()?{??
- ????????return?firstName;??
- ????}??
- ????public?void?setFirstName(String?firstName)?{??
- ????????this.firstName?=?firstName;??
- ????}??
- ????public?String?getLastName()?{??
- ????????return?lastName;??
- ????}??
- ????public?void?setLastName(String?lastName)?{??
- ????????this.lastName?=?lastName;??
- ????}??
- ????public?PhoneNumber?getPhonex()?{??
- ????????return?phonex;??
- ????}??
- ????public?void?setPhonex(PhoneNumber?phonex)?{??
- ????????this.phonex?=?phonex;??
- ????}??
- ????public?PhoneNumber?getFax()?{??
- ????????return?fax;??
- ????}??
- ????public?void?setFax(PhoneNumber?fax)?{??
- ????????this.fax?=?fax;??
- ????}??
- ???????
- }??
- </span>??
?
?PhoneNumber.java
?
Java代码??
- <span?style="font-size:?large;">package?com.xstream.test;??
- ??
- public?class?PhoneNumber?{??
- ??
- ????private?int?code;??
- ????private?int?number;??
- ??
- ????public?PhoneNumber(int?code,?int?number)?{??
- ????????this.code?=?code;??
- ????????this.number?=?number;??
- ????}??
- ??
- ????public?int?getCode()?{??
- ????????return?code;??
- ????}??
- ??
- ????public?void?setCode(int?code)?{??
- ????????this.code?=?code;??
- ????}??
- ??
- ????public?int?getNumber()?{??
- ????????return?number;??
- ????}??
- ??
- ????public?void?setNumber(int?number)?{??
- ????????this.number?=?number;??
- ????}??
- ??
- }??
- </span>??
?XstreamTest.java
?
Java代码??
- <span?style="font-size:?large;">package?com.xstream.test;??
- ??
- import?com.thoughtworks.xstream.XStream;??
- import?com.thoughtworks.xstream.io.xml.DomDriver;??
- ??
- public?class?XstreamTest?{??
- ??
- ????/**?
- ?????*??
- ?????*?使用xStream.alias(String?elementName,?Class?cls)为任何一个自定义类创建到类到元素的别名;?
- ?????*?使用xStream.toXML(Object?obj)转换对象到XML;?
- ?????*?使用xStream.fromXML(String?xml)转换XML到对象;?
- ?????*?@param?args?
- ?????*/??
- ????public?static?void?main(String[]?args)?{??
- ????????XStream?xStream?=?new?XStream(new?DomDriver());??
- ????????Person?joe?=?new?Person("Joe","Walnes");???
- ????????joe.setPhonex(new?PhoneNumber(123,222));???
- ????????joe.setFax(new?PhoneNumber(123,444));???
- ??????????
- ????????//这是可选的一步。没有这步XStream也可以很好的起作用,但是XML元素的名字就会包含??
- ????????//每个类的全称(包括包名),这将会使生成XML稍大。??
- ????????xStream.alias("person",Person.class);???
- ????????//序列化为XML??
- ????????String?xml=xStream.toXML(joe);??
- ????????System.out.println("对象序列化为XML:\n"+xml);??
- ????????//反序列化为对象??
- ????????Person?newJoe?=?(Person)xStream.fromXML(xml);???
- ????????System.out.println("XML反序列化为对象:\n"+newJoe);??
- ????}??
- ??????
- ??
- ??
- }??
- </span> ?