标签:
1 package com.example.myxmlmake; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Random; 8 9 import org.xmlpull.v1.XmlSerializer; 10 11 import com.example.myxmlmake.domain.SmsInfo; 12 13 import android.os.Bundle; 14 import android.os.Environment; 15 import android.app.Activity; 16 import android.util.Xml; 17 import android.view.Menu; 18 import android.view.View; 19 import android.widget.Toast; 20 21 public class MainActivity extends Activity { 22 23 private List<SmsInfo> smsInfos; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 smsInfos = new ArrayList<SmsInfo>(); 29 Random random = new Random(); 30 long number = 135000001; 31 for(int i=0;i<10;i++) 32 { 33 smsInfos.add(new SmsInfo(System.currentTimeMillis(),random.nextInt(2)+1,"短信内容"+i, Long.toString(number++))); 34 } 35 } 36 37 public void mybackup1(View view) 38 { 39 StringBuilder sb = new StringBuilder(); 40 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 41 sb.append("<smss>"); 42 for(SmsInfo info : smsInfos) 43 { 44 sb.append("<sms>"); 45 46 sb.append("<address>"); 47 sb.append(info.getAddress()); 48 sb.append("</address>"); 49 50 sb.append("<type>"); 51 sb.append(info.getType()); 52 sb.append("</type>"); 53 54 sb.append("<body>"); 55 sb.append(info.getBody()); 56 sb.append("</body>"); 57 58 sb.append("<date>"); 59 sb.append(info.getDate()); 60 sb.append("</date>"); 61 62 sb.append("</sms>"); 63 } 64 sb.append("</smss>"); 65 try{ 66 File file = new File(Environment.getExternalStorageDirectory(),"myback1.xml"); 67 FileOutputStream fos = new FileOutputStream(file); 68 fos.write(sb.toString().getBytes()); 69 fos.close(); 70 Toast.makeText(this, "生成成功!", 0).show(); 71 }catch(Exception e){ 72 e.printStackTrace(); 73 Toast.makeText(this, "生成失败!", 0).show(); 74 } 75 76 } 77 78 public void mybackup2(View view) 79 { 80 try 81 { 82 XmlSerializer serializer = Xml.newSerializer(); 83 File file = new File(Environment.getExternalStorageDirectory(),"myback2.xml"); 84 FileOutputStream os = new FileOutputStream(file); 85 serializer.setOutput(os,"utf-8"); 86 serializer.startDocument("utf-8", true); 87 serializer.startTag(null, "smss"); 88 89 for(SmsInfo info : smsInfos) 90 { 91 serializer.startTag(null, "sms"); 92 93 serializer.startTag(null, "body"); 94 serializer.text(info.getBody()); 95 serializer.endTag(null, "body"); 96 97 serializer.startTag(null, "address"); 98 serializer.text(info.getAddress()); 99 serializer.endTag(null, "address"); 100 101 serializer.startTag(null, "type"); 102 serializer.text(info.getType() + ""); 103 serializer.endTag(null, "type"); 104 105 serializer.startTag(null, "date"); 106 serializer.text(info.getDate() + ""); 107 serializer.endTag(null, "date"); 108 109 serializer.endTag(null, "sms"); 110 } 111 112 serializer.endTag(null, "smss"); 113 serializer.endDocument(); 114 os.close(); 115 Toast.makeText(this, "生成成功!", 0).show(); 116 }catch(Exception e){ 117 e.printStackTrace(); 118 Toast.makeText(this, "生成失败!", 0).show(); 119 } 120 } 121 @Override 122 public boolean onCreateOptionsMenu(Menu menu) { 123 // Inflate the menu; this adds items to the action bar if it is present. 124 getMenuInflater().inflate(R.menu.main, menu); 125 return true; 126 } 127 128 }
标签:
原文地址:http://www.cnblogs.com/zhongyinghe/p/5386542.html