在android平台中,我们经常会用到sharedPreference之类的东西,sharedPreference其实就是生成xml文件,然后去读写的过程。
下面请看一下代码目录:
源码如下:
package com.leo.writexml; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.xmlpull.v1.XmlSerializer; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Xml; import android.view.Menu; import android.view.View; import android.widget.Toast; import com.leo.writexml.domain.SmsInfo; public class MainActivity extends Activity { private List<SmsInfo> smsInfos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); smsInfos = new ArrayList<SmsInfo>(); Random random = new Random(); long number = 1580000001; for(int i = 0 ;i < 10 ;i++){ smsInfos.add(new SmsInfo(System.currentTimeMillis(),random.nextInt(2) + 1,"短信内容"+i,Long.toString(number + i),i)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } public void backupSms(View view){ //假设我已获得了所有的短信 StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); sb.append("<smss>"); for(SmsInfo info:smsInfos){ sb.append("<sms>"); sb.append("<body>"); sb.append(info.getBody()); sb.append("</body>"); sb.append("<date>"); sb.append(info.getDate()); sb.append("</date>"); sb.append("<type>"); sb.append(info.getType()); sb.append("</type>"); sb.append("<address>"); sb.append(info.getAddress()); sb.append("</address>"); sb.append("</sms>"); } sb.append("</smss>"); File file = new File(Environment.getExternalStorageDirectory(),"smsinfo.xml"); try { FileOutputStream fos = new FileOutputStream(file); fos.write(sb.toString().getBytes()); fos.close(); Toast.makeText(this,"备份 成功",0).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this,"备份失败",0).show(); } } /** * 第二种方式来生成xml文件 * @param view */ public void backupSms2(View view){ XmlSerializer serializer = Xml.newSerializer();//xml序列化 try { File file = new File(Environment.getExternalStorageDirectory(),"backupsms2.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos,"utf-8"); serializer.startDocument("utf-8",true); serializer.startTag(null,"smss"); for(SmsInfo info :smsInfos){ serializer.startTag(null,"sms"); serializer.attribute(null,"id",info.getId()+""); serializer.startTag(null,"body"); serializer.text(info.getBody()); serializer.endTag(null,"body"); serializer.startTag(null,"address"); serializer.text(info.getAddress()); serializer.endTag(null,"address"); serializer.startTag(null,"type"); serializer.text(info.getType()+""); serializer.endTag(null,"type"); serializer.startTag(null,"date"); serializer.text(info.getDate()+""); serializer.endTag(null,"date"); serializer.endTag(null,"sms"); } serializer.endTag(null,"smss"); serializer.endDocument(); fos.close(); Toast.makeText(this,"备份成功",0).show(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(this,0).show(); } } }smsinfo文件:
package com.leo.writexml.domain; public class SmsInfo { private long date; private int type; private String body; private String address; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public SmsInfo() { } public SmsInfo(long date,int type,String body,String address,int id) { super(); this.date = date; this.type = type; this.body = body; this.address = address; this.id = id; } public long getDate() { return date; } public void setDate(long date) { this.date = date; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }androidmanifist.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.leo.writexml" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.leo.writexml.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>activity_main.xml:
<RelativeLayout xmlns: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=".MainActivity" > <Button android:id="@+id/btn" android:onClick="backupSms" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="199dp" android:text="生成XML文件" /> <Button android:layout_below="@id/btn" android:onClick="backupSms2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二种方式生成XML文件" /> </RelativeLayout>原文链接:https://www.f2er.com/xml/299609.html