Android XMl parsing magic xstream 2: Converting objects into xml, androidxstream
For more information about xstream, see android XMl parsing magic xstream 1: parsing the aa. xml file in the asset folder of the android project.
1. Javabeen code
package com.android10;public class Product { private String name ; private String age ; 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; }}
2. Operate instances
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); Product product = new Product (); product. setName ("jhon"); product. setAge ("30"); 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 Effect
<Com. android10.Product>
<Age> 30 </age>
<Name> jhon </name>
</Com. android10.Product>
<Blog>
<Age> 30 </age>
<Name> jhon </name>
</Blog>
4. Notes
During the test, we found that System. out. println ("sss" + string); and System. out. println ("sss" + string2 );
The output is:
Sss <com. android10.Product>
Sss <blog>
At first, I thought it was wrong. Finally, when debugging the debug breakpoint, I found the value of string:
<Com. android10.Product>
<Age> 30 </age>
<Name> jhon </name>
</Com. android10.Product>
It is concluded that the eclipse console cannot output strings in xml format.
5. Download the project
Http://download.csdn.net/detail/yanzi2015/8798893