Android XMl parsing magic xstream 3: Converting complex objects into xml and androidxstream
For more information about xstream, see:
Android XMl parsing magic xstream 1: parse the aa. xml file in the asset folder of the android Project
Android XMl parsing magic xstream 2: Converting objects into xml
1. JavaBeen
package com.android10;public class Person { String pName ; String pAge ; public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } public String getpAge() { return pAge; } public void setpAge(String pAge) { this.pAge = pAge; } }
package com.android10;public class Product { private String name ; private String age ; private Person person ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; }}
2. Main Methods
Package com. android10; import android. app. activity; import android. OS. bundle; import com. thoughtworks. xstream. XStream; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Person person = new Person (); person. setpName ("saliy"); person. setpAge ("36"); Product product Product = new product (); Product. setName ("jhon"); product. setAge ("30"); product. setPerson (person); System. out. println ("sss" + product. getName () + product. getAge (); // convert the object to XML XStream xstream1 = new XStream (); String string = xstream1.toXML (product); System. out. println ("sss" + string); // converts the object to XML, and sets the alias XStream xstream2 = new XStream (); xstream2.alias ("blog", Product. class); // modify the alias String string2 = xstream2.toXML (product); System. out. println ("sss" + string2 );}}
3. Running 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. Download Code
Http://download.csdn.net/detail/yanzi2015/8800411