xml文件的序列化示例

时间:2022-09-19 20:35:51

1、创建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"
tools:context=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="99dp"
android:onClick="bck"
android:text="@string/bcak" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_centerVertical="true"
android:onClick="pck"
android:text="@string/back2" /> </RelativeLayout>

2、编写MainActivity类

package com.hyzhou.xmldemo;

import java.io.File;
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.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { private List<SMSInfo> smsinfo; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
smsinfo=new ArrayList<SMSInfo>();
Random random=new Random();
long number=1571132;
for (int i = 0; i < 10; i++) {
smsinfo.add(new SMSInfo(System.currentTimeMillis(), random.nextInt()+1, "短信内容"+i, Long.toString(number+i),i));
} }
//第一种实现方式
public void bck(View view) {
StringBuilder sb=new StringBuilder();
sb.append("<smss>");
for (SMSInfo info : smsinfo) {
sb.append("<sms>");
sb.append("<address>");
sb.append(info.getAddress());
sb.append("</address>"); sb.append("<type>");
sb.append(info.getType());
sb.append("</type>"); sb.append("<body>");
sb.append(info.getBody());
sb.append("</body>"); sb.append("<date>");
sb.append(info.getBody());
sb.append("</date>"); sb.append("</sms>"); }
sb.append("</smss>"); try {
File file=new File(Environment.getExternalStorageDirectory(),"back.xml");
FileOutputStream fos=new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
Toast.makeText(this, "备份成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "备份失败", Toast.LENGTH_SHORT).show(); }
}
//第二种实现方式,推荐使用第二种方式
public void pck(View view) {
try {
XmlSerializer serializer=Xml.newSerializer();
File file=new File(Environment.getExternalStorageDirectory(),"back2.xml");
FileOutputStream os = new FileOutputStream(file);
//初始化序列号器指定xml数据写到哪个文件,并且指定文件的编码方式
serializer.setOutput(os, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss"); for (SMSInfo info : smsinfo) {
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, "data");
serializer.text(info.getDate()+"");
serializer.endTag(null, "data");
serializer.endTag(null, "sms"); }
serializer.endTag(null, "smss");
serializer.endDocument();
os.close();
Toast.makeText(this, "备份2成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} }

3、建立SMSInfo类

/**
*
*/
package com.hyzhou.xmldemo; public class SMSInfo { private long date;
private int type;
private String body;
private String address;
private int 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;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public SMSInfo() {
super();
// TODO Auto-generated constructor stub
} 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;
} }

附件:
back.xml结果展示

<?xml version="1.0"?>
-<smss>-<sms><address>1571132</address><type>-544540687</type><body>短信内容0</body><date>短信内容0</date></sms>-<sms><address>1571133</address><type>-929821098</type><body>短信内容1</body><date>短信内容1</date></sms>-<sms><address>1571134</address><type>-467001032</type><body>短信内容2</body><date>短信内容2</date></sms>-<sms><address>1571135</address><type>-473096343</type><body>短信内容3</body><date>短信内容3</date></sms>-<sms><address>1571136</address><type>-1370139761</type><body>短信内容4</body><date>短信内容4</date></sms>-<sms><address>1571137</address><type>507788169</type><body>短信内容5</body><date>短信内容5</date></sms>-<sms><address>1571138</address><type>-995243383</type><body>短信内容6</body><date>短信内容6</date></sms>-<sms><address>1571139</address><type>-1178397444</type><body>短信内容7</body><date>短信内容7</date></sms>-<sms><address>1571140</address><type>964211414</type><body>短信内容8</body><date>短信内容8</date></sms>-<sms><address>1571141</address><type>103006854</type><body>短信内容9</body><date>短信内容9</date></sms></smss>

back2.xml结果展示

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<smss>-<sms id="0"><body>短信内容0</body><address>1571132</address><type>-812319933</type><data>1381977444649</data></sms>-<sms id="1"><body>短信内容1</body><address>1571133</address><type>-1196295931</type><data>1381977444650</data></sms>-<sms id="2"><body>短信内容2</body><address>1571134</address><type>-1295888851</type><data>1381977444650</data></sms>-<sms id="3"><body>短信内容3</body><address>1571135</address><type>16784268</type><data>1381977444650</data></sms>-<sms id="4"><body>短信内容4</body><address>1571136</address><type>1005445253</type><data>1381977444650</data></sms>-<sms id="5"><body>短信内容5</body><address>1571137</address><type>1067452916</type><data>1381977444650</data></sms>-<sms id="6"><body>短信内容6</body><address>1571138</address><type>-293587603</type><data>1381977444650</data></sms>-<sms id="7"><body>短信内容7</body><address>1571139</address><type>1221438355</type><data>1381977444650</data></sms>-<sms id="8"><body>短信内容8</body><address>1571140</address><type>-1126016278</type><data>1381977444651</data></sms>-<sms id="9"><body>短信内容9</body><address>1571141</address><type>314137807</type><data>1381977444651</data></sms></smss>

apk下载