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

Source: Internet
Author: User

Android XMl parsing magic xstream 3: Converting complex objects into xml and 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

 

1. 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. Main Methods

Package com. android10; import android. app. activity; import android. OS. bundle; 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); System. out. println ("sss" + product. getName () + product. getAge (); // convert the object to XML XStream xstream1 = new XStream (); String string = xstream1.toXML (product); System. out. println ("sss" + string); // converts the object to XML, and sets the alias XStream xstream2 = new XStream (); xstream2.alias ("blog", Product. class); // modify the alias String string2 = xstream2.toXML (product); System. out. println ("sss" + string2 );}}

 

3. Running 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. Download Code

Http://download.csdn.net/detail/yanzi2015/8800411

 

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.