Preface: For XStream do not understand, please see:
Android XMl parsing Magic XStream: Parse aa.xml file under Asset folder in Android project
Android XML parsing Magic XStream II: Converting objects to XML
1, Javabeen
PackageCom.android10; Public classPerson {String pName; String PAge; PublicString Getpname () {returnPName; } Public voidsetpname (String pName) { This. PName =PName; } PublicString GetPage () {returnPAge; } Public voidsetpage (String pAge) { This. PAge =PAge; } }
PackageCom.android10; Public classProduct {PrivateString name; PrivateString age; Privateperson person ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; } PublicPerson Getperson () {returnPerson ; } Public voidSetperson (person person) { This. person =Person ; }}
2. Main methods
PackageCom.android10;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportCom.thoughtworks.xstream.XStream; Public classMainactivityextendsActivity {@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Person Person=NewPerson (); Person.setpname ("Saliy" ) ; Person.setpage ("36" ); Product Product=NewProduct (); Product.setname ("Jhon" ) ; Product.setage ("30" ); Product.setperson (person); System.out.println ("SSS" + product.getname () +product.getage ()); //Convert an object to XMLXStream xstream1 =NewXStream (); String String=xstream1.toxml (product); System.out.println ("SSS" +string); //Convert the object to XML and set the aliasXStream xstream2 =NewXStream (); Xstream2.alias ("Blog", Product.class) ;//Modify AliasesString string2 =xstream2.toxml (product); System.out.println ("SSS" +string2); }}
3. Operation Result
<com.android10.Product> <age>30</age> <name>jhon</name> <person > <pAge>36</pAge> <pName>saliy</pName> </person></ com.android10.product><blog> <age>30</age> <name>jhon</name> <person> <pAge>36</pAge> <pName>saliy</pName> </person>< /blog>
4. Code download
http://download.csdn.net/detail/yanzi2015/8800411
Android XML parsing Magic XStream Three: Transforming complex objects into XML