Android generates an XML file

Source: Internet
Author: User

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.generatexml.MainActivity" >    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "SMS Backup 1"Android:id= "@+id/btn1"Android:onclick= "BackupSms1" />        <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "SMS Backup 2"Android:layout_below= "@id/btn1"Android:onclick= "BackupSms2" /></Relativelayout>

 PackageCom.example.generatexml;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.xmlpull.v1.XmlSerializer;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.support.v7.app.ActionBarActivity;Importandroid.util.Xml;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Toast;ImportCom.example.generatexml.domain.SmsInfo; Public classMainactivityextendsactionbaractivity {PrivateList<smsinfo>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); List=NewArraylist<smsinfo>(); LongPhone = 1880075555;  for(inti=0;i<10;i++) {Smsinfo info=NewSmsinfo (i, System.currenttimemillis (), "< SMS Content" +i,phone+i+ ", i/2+1);        List.add (info); }    }             Public voidbackupSms1 (View v) {Try{File File=NewFile (Environment.getexternalstoragedirectory (), "Backup1.xml"); FileOutputStream Fos=Newfileoutputstream (file); StringBuffer SB=NewStringBuffer (); Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"); Sb.append ("<smss>");  for(Smsinfo s:list) {sb.append ("<sms>"); Sb.append ("<body>" +s.getbody () + "</body>"); Sb.append ("<type>" +s.gettype () + "</type>"); Sb.append ("<addr>" +s.getaddr () + "</addr>"); Sb.append ("<date>" +s.getdate () + "</date>"); Sb.append ("</sms>"); } sb.append ("</smss>");            Fos.write (Sb.tostring (). GetBytes ());            Fos.close (); Toast.maketext ( This, "saved successfully", 0). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "Save Failed", 0). Show (); }                            }         Public voidBackupSms2 (View v) {Try{XmlSerializer ser=Xml.newserializer (); File File=NewFile (Environment.getexternalstoragedirectory (), "Backup2.xml"); FileOutputStream OS=Newfileoutputstream (file); Ser.setoutput (OS,"Utf-8"); Ser.startdocument ("Utf-8",true); Ser.starttag (NULL, "SMSs");  for(Smsinfo s:list) {Ser.starttag (NULL, "SMS"); Ser.attribute (NULL, "id", S.getid () + ""); Ser.starttag (NULL, "Body");                Ser.text (S.getbody ()); Ser.endtag (NULL, "Body"); Ser.starttag (NULL, "type"); Ser.text (S.gettype ()+""); Ser.endtag (NULL, "type"); Ser.starttag (NULL, "addr");                Ser.text (S.getaddr ()); Ser.endtag (NULL, "addr"); Ser.starttag (NULL, "Date"); Ser.text (S.getdate ()+""); Ser.endtag (NULL, "Date"); Ser.endtag (NULL, "SMS"); } Ser.endtag (NULL, "SMSs");                        Ser.enddocument ();                        Os.close (); Toast.maketext ( This, "saved successfully", 0). Show (); } Catch(Exception e) {e.printstacktrace (); Toast.maketext ( This, "Save Failed", 0). Show (); }    }}

Android generates an XML file

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.