这个例子是参照官网上面的例子写的:
http://xstream.codehaus.org/tutorial.html
1.首先构造想要序列化的类
Person.java:
- publicclassPerson{
- privateStringfirstname;
- privateStringlastname;
- privatePhoneNumberphone;
- privatePhoneNumberfax;
- //...constructorsandmethods
- publicPerson(Stringfirstname,Stringlastname){
- super();
- this.firstname=firstname;
- this.lastname=lastname;
- }
- voidsetPhone(PhoneNumberphoneNumber){
- this.phone=phoneNumber;
- }
- voidsetFax(PhoneNumberphoneNumber){
- this.fax=phoneNumber;
- @Override
- publicStringtoString(){
- return"FirstName:"+firstname+",LastName:"+lastname
- +"phone.Code:"+phone.getCode()+"phone.Number:"
- +phone.getNumber()+"fax:"+fax.getCode()+"fax.Number:"
- +fax.getNumber();
- }
PhoneNumber.java:
copy
- classPhoneNumber{
- privateintcode;
- privateStringnumber;
- publicPhoneNumber(intcode,Stringnumber){
- this.code=code;
- this.number=number;
- intgetCode(){
- returncode;
- publicStringgetNumber(){
- returnnumber;
- }