Android XML parsing Magic XStream Five: Transforming complex objects into XML and writing XML files to SD cards

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

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

Android XML parsing Magic XStream Four: parsing complex XMl files into objects

1, establish 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. Tool Class Code

 PackageCom.android10;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;ImportAndroid.content.Context;Importandroid.os.Environment; Public classXstreamutil {xcallback xcallback; /*** Write XML string to SD card file *@paramContext *@paramstr XML string*/     Public voidWritetoxml (context context, String str) {//Get file pathString Sdpath = environment.getexternalstoragedirectory () + "/myfile1.xml/" ; //Create a fileFile File =NewFile (Sdpath); if( !file.exists ()) {            Try{file.createnewfile (); } Catch(IOException e) {e.printstacktrace (); }         }        //Write Data        Try{FileOutputStream out=Newfileoutputstream (file); OutputStreamWriter OUTW=NewOutputStreamWriter (out); Try{outw.write (str);                  Outw.close ();                  Out.close ();             Xcallback.success (); } Catch(IOException e) {xcallback.fail (); }          } Catch(FileNotFoundException E1) {e1.printstacktrace ();         Xcallback.fail (); }    }     voidSetxstreamlister (Xcallback xcallback) { This. Xcallback =Xcallback; }}Interfacexcallback{/*** Write Success*/    voidsuccess (); /*** Write Failed*/    voidfail (); }


3. Main methods

 PackageCom.android10;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast;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); //To convert an object to an XML stringXStream XStream =NewXStream (); //Setting AliasesXstream.alias ("blog", Product.class ) ; String String=xstream.toxml (product); Xstreamutil Xutil=NewXstreamutil (); Xutil.setxstreamlister (NewXcallback () {@Override Public voidsuccess () {Toast.maketext (mainactivity. This, "succeeded.", Toast.length_short). Show (); } @Override Public voidfail () {Toast.maketext (mainactivity. This, "failed.", Toast.length_short). Show ();        }        }); Xutil.writetoxml ( This, String); }}

4. Operation result

<blog>
<age>30</age>
<name>jhon</name>
<person>
<pAge>36</pAge>
<pName>saliy</pName>
</person>
</blog>

Android XML parsing Magic XStream Five: Transforming complex objects into XML and writing XML files to SD cards

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.