Android generates xml files and android generates xml files.
Generate an XML file backup text message in the following format:
<? Xml version = "1.0" encoding = "UTF-8" standalone = "true"?>
<Message>
<Sms> <body> 0th text messages </body> <date> 1465041254178 </date> <address> 000 </address> <type> 1 </type> </sms>
<Sms> <body> 1st text messages </body> <date> 1465041254179 </date> <address> 111 </address> <type> 1 </type> </sms>
<Sms> <body> 2nd text messages </body> <date> 1465041254179 </date> <address> 222 </address> <type> 1 </type> </sms> </message>
Create an Sms class
package com.wuyudong.createxml.domain;public class Sms { private String body; private String date; private String type; private String address; public Sms(String body, String date, String type, String address) { super(); this.body = body; this.date = date; this.type = type; this.address = address; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
There are several virtual text message objects in the list. Backup Data is usually backed up to the SD card.
Concatenate strings using StringBuffer, * append all nodes of the xml file to the sb object.
Sb. append ("<? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> ");
// Add the Start Node of smss
Sb. append ("<smss> ");
.......
* Write sb to the output stream
Fos. write (sb. toString (). getBytes ());
The complete code is as follows:
Package com. wuyudong. createxml; import java. io. file; import java. io. fileOutputStream; import java. util. arrayList; import java. util. list; import com. wuyudong. createxml. domain. sms; import android. OS. bundle; import android. OS. environment; import android. app. activity; import android. view. menu; import android. view. view; public class MainActivity extends Activity {List <Sms> message; @ Override protected void onCrea Te (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create 10 virtual Sms messages: message = new ArrayList <Sms> (); for (int I = 0; I <10; I ++) {Sms sms = new Sms ("no." + I + "Sms", System. currentTimeMillis () + "", "1", "" + I); message. add (sms) ;}} public void click (View v) {File file = new File (Environment. getExternalStorageDirectory (), "backup. xml" ); Try {FileOutputStream fos = new FileOutputStream (file); StringBuffer sb = new StringBuffer (); // Add the xml header sb. append ("<? Xml version = '1. 0' encoding = 'utf-8' standalone = 'Yes'?> "); // Add the root node sb. append ("<message>"); // Add an sms node for (Sms sms: message) {sb. append ("<sms>"); sb. append ("<body>"); sb. append (sms. getBody (); sb. append ("</body>"); sb. append ("<date>"); sb. append (sms. getDate (); sb. append ("</date>"); sb. append ("<address>"); sb. append (sms. getAddress (); sb. append ("</address>"); sb. append ("<type>"); sb. append (sms. getType (); sb. append ("</type>"); sb. append ("</sms>");} sb. append ("</message>"); fos. write (sb. toString (). getBytes (); fos. close ();} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}}