Foreword: The XStream does not understand, see: Android XMl parsing Magic XStream: Parse the Aa.xml file under Asset folder in Android project
1. Javabeen Code
PackageCom.android10; Public classProduct {PrivateString name; PrivateString age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; }}
2. Operation Example
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); Product Product=NewProduct (); Product.setname ("Jhon" ) ; Product.setage ("30" ); 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 effect
<com.android10.Product>
<age>30</age>
<name>jhon</name>
</com.android10.Product>
<blog>
<age>30</age>
<name>jhon</name>
</blog>
4. Precautions
In the test, found System.out.println ("SSS" + string); and System.out.println ("SSS" + string2);
The output is:
Sss<com.android10.product>
Sss<blog>
I started to think where it was written wrong, and finally, when debugging the debug breakpoint, I found the value of string:
<com.android10.Product>
<age>30</age>
<name>jhon</name>
</com.android10.Product>
The result is that the Eclipse console cannot output a string in XML format
Android XML parsing Magic XStream II: Converting objects to XML