packagecom.nexus.XStreamTest; importjava.io.Writer; importjava.util.ArrayList; importjava.util.List; importcom.nexus.XStream.Address; importcom.nexus.XStream.Person; importcom.nexus.XStream.Profile; importcom.thoughtworks.xstream.XStream; importcom.thoughtworks.xstream.core.util.QuickWriter; importcom.thoughtworks.xstream.io.HierarchicalStreamWriter; importcom.thoughtworks.xstream.io.xml.PrettyPrintWriter; importcom.thoughtworks.xstream.io.xml.XppDriver; publicclasstest{ /** *@paramargs */ publicstaticvoidmain(String[]args){ /*Useruser=newUser(); user.setId("id"); user.setName("name"); user.setUrl("url"); xstream.alias("xml",user.getClass()); //在处理对象的方法中添加xstream.autodetectAnnotations(true);这样就可以解决此类问题 //xstream.autodetectAnnotations(true); System.out.println(xstream.toXML(user));*/ Addressaddress1=newAddress("郑州市经三路","450001"); Addressaddress2=newAddress("西安市雁塔路","710002"); List<Address>addList=newArrayList<Address>(); addList.add(address1); addList.add(address2); Profileprofile=newProfile("软件工程师","13512129933","备注说明"); Personperson=newPerson("熔岩","27",profile,addList); xstream.alias("PERSON",Person.class); xstream.alias("PROFILE",Profile.class); xstream.alias("ADDRESS",Address.class); System.out.println(xstream.toXML(person)); } /** *初始化,封装应用 */ privatestaticXStreamxstream=newXStream(newXppDriver(){ publicHierarchicalStreamWritercreateWriter(Writerout){ returnnewPrettyPrintWriter(out){ publicvoidstartNode(Stringname,Classclazz){ super.startNode(name); } protectedvoidwriteText(QuickWriterwriter,Stringtext){ /**自定义格式化**/ writer.write(text); } }; } }); }
packagecom.nexus.XStreamTest; importcom.thoughtworks.xstream.annotations.XStreamOmitField; publicclassUser{ //在忽略的字段属性或者方法上添加@XStreamOmitField注解 @XStreamOmitField privateStringid; privateStringname; privateStringurl; publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetUrl(){ returnurl; } publicvoidsetUrl(Stringurl){ this.url=url; } }
packagecom.nexus.XStream; importjava.util.List; publicclassperson{ privateStringname; privateStringage; privateProfileprofile; privateList<Address>addlist; publicPerson(Stringname,Stringage,Profileprofile,List<Address>addlist){ this.name=name; this.age=age; this.profile=profile; this.addlist=addlist; } publicStringtoString(){ return"Person{"+ "name='"+name+'\''+ ",age='"+age+'\''+ ",profile="+profile+ ",addlist="+addlist+ '}'; } }
packagecom.nexus.XStream; publicclassprofile{ privateStringjob; privateStringtel; privateStringremark; publicProfile(Stringjob,Stringtel,Stringremark){ this.job=job; this.tel=tel; this.remark=remark; } publicStringtoString(){ return"Profile{"+ "job='"+job+'\''+ ",tel='"+tel+'\''+ ",remark='"+remark+'\''+ '}'; } }
packagecom.nexus.XStream; publicclassAddress{ privateStringadd; privateStringzipcode; publicAddress(Stringadd,Stringzipcode){ this.add=add; this.zipcode=zipcode; } publicStringtoString(){ return"Address{"+ "add='"+add+'\''+ ",zipcode='"+zipcode+'\''+ '}'; } }