Android XML parsing Magic XStream II: Converting objects to XML

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.