Xmlserializer serialization XML for Android (write)

Source: Internet
Author: User

First, let's take a look at what is serializer. serializer is serialized, also known as serialization. It does not simply store the object in the memory, it allows us to transfer objects in the stream, so that the object can be transferred as the basic data.

Finally

Paste the main code:

Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/textview" <br/> </linearlayout> <br/>

Activity Code

 

Package cn.com. xmlseriliazer; </P> <p> Import Java. io. stringwriter; <br/> Import Java. util. arraylist; </P> <p> Import Org. xmlpull. v1.xmlpullparserfactory; <br/> Import Org. xmlpull. v1.xmlserializer; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. textview; <br/>/** <br/> * @ author chenzheng_java <br/> * @ Description: generate an XML file through xmlserilizer. <br/> * @ since 2011/03/03 <br/> */<br/> public class xmlserializeractivity extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> string result = producexml (); <br/> textview = (textview) This. findviewbyid (R. id. textview); <br/> textview. settext (result); </P> <p >}< br/>/** <br/> * <Br/> * @ return: String Representation of the XML file generated <br/> */<br/> private string producexml () {</P> <p> stringwriter = new stringwriter (); <br/> arraylist <beauty> beautylist = getdata (); <br/> try {<br/> // obtain the xmlserializer object <br/> xmlpullparserfactory factory = xmlpullparserfactory. newinstance (); <br/> xmlserializer = factory. newserializer (); <br/> // set the output stream object <br/> xmlserializer. setoutput (St Ringwriter); <br/>/* <br/> * startdocument (string encoding, Boolean standalone) encoding indicates the encoding method <br/> * standalone indicates whether the file calls other external files. <Br/> * If the value is yes, no external rule file is called. If the value is no, an external rule file is called. The default value is "yes ". <Br/> */<br/> xmlserializer. startdocument ("UTF-8", true); <br/> xmlserializer. starttag (null, "beauties"); <br/> for (Beauty: beautylist) {<br/>/* <br/> * starttag (string namespace, string name) the namespace here is used to uniquely identify the XML tag <br/> * the XML namespace attribute is placed in the start tag of an element, and the following syntax is used: <br/> xmlns: namespace-Prefix = "namespaceuri" <br/> when a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace. <Br/> Note: The address used to indicate the namespace is not used by the parser to find information. The only function is to assign a unique namespace name. However, many companies often use namespaces as pointers to point to a real-stored webpage, which contains information about namespaces. <Br/> */<br/> xmlserializer. starttag (null, "beauty"); </P> <p> xmlserializer. starttag (null, "name"); <br/> xmlserializer. text (beauty. getname (); <br/> xmlserializer. endtag (null, "name"); </P> <p> xmlserializer. starttag (null, "Age"); <br/> xmlserializer. text (beauty. getage (); <br/> xmlserializer. endtag (null, "Age"); </P> <p> xmlserializer. endtag (null, "beauty"); <br/>}< br/> xmlserializer. endtag (null, "beauties"); <br/> xmlserializer. enddocument (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return stringwriter. tostring (); </P> <p >}</P> <p>/** <br/> * @ return contains a collection of beauty information. <br/> */<br/> private arraylist <beauty> getdata () {<br/> arraylist <beauty> beautylist = new arraylist <beauty> (); </P> <p> beauty Yangmi = new beauty ("Yang Mi ", "23"); <br/> beauty linzhiling = new beauty ("Lin Zhiling", "28"); </P> <p> beautylist. add (Yangmi); <br/> beautylist. add (linzhiling); </P> <p> return beautylist; <br/>}</P> <p>/** <br/> * @ author chenzheng_java <br/> * Beauty entity class <br/> */<br/> private class beauty {<br/> string name; <br/> string age; <br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String getage () {<br/> return age; <br/>}< br/> Public void setage (string age) {<br/> This. age = age; <br/>}< br/> @ override <br/> Public String tostring () {<br/> return "Beauty [age =" + age + ", name =" + name + "]"; <br/>}< br/> Public beauty (string name, string age) {<br/> This. name = Name; <br/> This. age = age; <br/>}< br/> Public beauty () {</P> <p >}</P> <p>} 

Others are default.

We can see from the code that it is quite easy to generate xml using xmlserializer. The basic steps are similar to parsing XML. I won't talk much here. For more information, see API.

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.