Android Beginner Tutorial: Using the XML sequencer

Source: Internet
Author: User

Before backing up the text message generated by the generation of XML is manually spelled, there is a problem: when the text message in the presence of </body> such a label, the final result is not a complete XML file, obviously error. However, the way the serializer is used today can effectively solve the problems encountered on top. Just look at how the serializer generates the XML file:

Define a layout file for the activity, with the knowledge simply adding a button:

<button        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text= " Spare SMS "         android:onclick=" click "        />

See the code in the main activity as follows:

Package Com.itydl.xmlserializer;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException; Import Java.util.arraylist;import Java.util.list;import Org.xmlpull.v1.xmlserializer;import Com.itydl.createxml.domain.message;import Android.os.bundle;import Android.os.environment;import Android.app.activity;import Android.content.context;import Android.util.xml;import Android.view.Menu;import android.view.view;//to better preserve different, and responsible data, using the XML Sequencer public class Mainactivity extends Activity {list<message> smslist ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//virtual 10 SMS Smslist = new arraylist<message> (); for (int i = 0; i <; i++) {Message SMS = New Message ("Little chi Good" + I, System.currenttimemillis () + "", "138" + i + I, "1"); Smslist.add (SMS);}} public void Click (View v) {//uses an XML serializer to generate an XML file. The serializer filters out the clutter (tagged attributes in text messages)//1. Gets the Serializer object xmlserializer xs = Xml.newserializer ();//2. initialization. Because the Xml file is involved, initialization alsoInclude file name and path file File = new file (Environment.getexternalstoragedirectory (), "Sms.xml"); try {FileOutputStream fos = new FileOutputStream (file);//enconding: Specifies what encoding is used to generate the XML file Xs.setoutput (FOS, "utf-8");//setoutput (OutputStream OS, String encoding), encoding by default U8//3. Start generating XML file//enconding: Specify the value of the Enconding attribute in the header node xs.startdocument ("Utf-8", true);//Generate head node (highest row), Startdocument (String encoding, Boolean standalone) Xs.starttag (null, "message");//generates a root node that starts the label node for (Message SMS: Smslist) {Xs.starttag (null, "SMS"), Xs.starttag (null, "body"),//start and end a pair of writes. There are also four sub-nodes Xs.text (sms.getbody () + "<body>");//This will not be an error. Instead, the <body> is treated as content Xs.endtag (null, "body"), Xs.starttag (null, "date"), Xs.text (Sms.getdate ()), Xs.endtag (null, " Date "), Xs.starttag (null," type "), Xs.text (Sms.gettype ()), Xs.endtag (null," type "), Xs.starttag (NULL," address "); Xs.text (Sms.getaddress ()), Xs.endtag (NULL, "address"), Xs.endtag (NULL, "SMS"); Xs.endtag (NULL, "message");//tells the serializer that the file has been generated xs.enddocument ();} catch (Exception e) {//TODO auto-generated catch Blocke. Printstacktrace ();} }}

Of course, writing to SDcard requires write permission. You can join:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

Run the program and click the Backup button. We can find this file in the SDcard directory:

Export look inside the format is as follows:

After omitting, the resulting XML file is no problem. See no errors even if <body> tags appear in the text

Android Beginner Tutorial: Using the XML sequencer

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.