Android XMl parsing magic xstream 5: Convert complex objects into xml, and write the xml file in the SD card, androidxstream

Source: Internet
Author: User

Android XMl parsing magic xstream 5: Convert complex objects into xml, and write the xml file in the SD card, androidxstream

For more information about xstream, see:

Android XMl parsing magic xstream 1: parse the aa. xml file in the asset folder of the android Project

Android XMl parsing magic xstream 2: Converting objects into xml

Android XMl parsing magic xstream 3: Converting complex objects into xml

Android XMl parsing magic xstream 4: parsing complex xml files as objects

 

1. Create a JavaBeen

package com.android10;public class Person {    String pName ;    String pAge  ;        public String getpName() {        return pName;    }    public void setpName(String pName) {        this.pName = pName;    }    public String getpAge() {        return pAge;    }    public void setpAge(String pAge) {        this.pAge = pAge;    } }

 

package com.android10;public class Product {    private String name ;    private String age  ;    private Person person ;    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;    }    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }}


2. Tool code

Package com. android10; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStreamWriter; import android. content. context; import android. OS. environment; public class XstreamUtil {XcallBack xcallBack;/*** write the xml string to the SD card file * @ param context * @ param str xml string */public void writeToXml (Context context, string str) {// get File path String SDPATH = Environment. getExternalStorageDirectory () + "/myfile1.xml/"; // create a file File = new file (SDPATH); if (! File. exists () {try {file. createNewFile ();} catch (IOException e) {e. printStackTrace () ;}// write data try {FileOutputStream out = new FileOutputStream (file); OutputStreamWriter outw = new OutputStreamWriter (out); try {outw. write (str); outw. close (); out. close (); xcallBack. success ();} catch (IOException e) {xcallBack. fail () ;}} catch (FileNotFoundException e1) {e1.printStackTrace (); xcallBack. fail () ;}} void setXStreamLister (XcallBack xcallBack) {this. xcallBack = xcallBack;} interface XcallBack {/*** Write Succeeded */void success ();/*** write failed */void fail ();}


3. Main Methods

Package com. android10; import android. app. activity; import android. OS. bundle; import android. widget. toast; import com. thoughtworks. xstream. XStream; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Person person = new Person (); person. setpName ("saliy"); person. setpAge ("36"); Product product Product = new product (); Product. setName ("jhon"); product. setAge ("30"); product. setPerson (person); // convert the object to an xml string XStream xstream = new XStream (); // set the alias xstream. alias ("blog", Product. class); String string = xstream. toXML (product); XstreamUtil xUtil = new XstreamUtil (); xUtil. setXStreamLister (new XcallBack () {@ Override public void success () {Toast. makeText (MainActivity. this, "succeeded", Toast. LENGTH_SHORT ). show () ;}@ Override public void fail () {Toast. makeText (MainActivity. this, "failed", Toast. LENGTH_SHORT ). show () ;}}); xUtil. writeToXml (this, string );}}

 

4. Running result

 

<Blog>
<Age> 30 </age>
<Name> jhon </name>
<Person>
<PAge> 36 </pAge>
<PName> saliy </pName>
</Person>
</Blog>

 

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.