Android XML parsing Magic XStream Three: Transforming complex objects into XML

Source: Internet
Author: User

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

Related Article

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.