android中XML文件的生成与读取

时间:2022-04-06 12:05:43

一、项目目录截图

android中XML文件的生成与读取

二、layout界面

android中XML文件的生成与读取

三、layout界面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="com.zgs.message.MainActivity" >

<Button
android:id="@+id/bt_backup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/txt_backup" />

<Button
android:id="@+id/bt_restore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bt_backup"
android:text="@string/txt_restore" />

</RelativeLayout>
四、MainActivity代码
package com.zgs.message;

import com.zgs.message.util.SmsUtils;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;

Button bt_backup = (Button) findViewById(R.id.bt_backup);
Button bt_restore = (Button) findViewById(R.id.bt_restore);

bt_backup.setOnClickListener(this);
bt_restore.setOnClickListener(this);

}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.bt_backup:
if(SmsUtils.backupSms_android(mContext)){
Toast.makeText(mContext, "短信备份成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mContext, "短信备份失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_restore:
int result = SmsUtils.restoreSms_android(mContext);
Toast.makeText(mContext, "成功恢复"+result+"条短信", Toast.LENGTH_SHORT).show();
break;
default:
break;
}

}


}

五、SmsUtils类代码

dom解析:基于全文加载的解析方式 sax解析:基于事件的逐行解析方式 pull解析:同sax

package com.zgs.message.util;

import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import com.zgs.message.bean.SmsBean;
import com.zgs.message.dao.SmsDao;

import android.content.Context;
import android.util.Xml;

public class SmsUtils {

//使用XmlSerializer来序列化xml文件
public static boolean backupSms_android(Context mContext) {
try{

//0.获取短信数据
ArrayList<SmsBean> allSms = SmsDao.getAllSms();
//1.通过Xml获取一个XmlSerializer对象
XmlSerializer xs = Xml.newSerializer();
//2.设置XmlSerializer的一些参数,比如:设置xml写入到哪个文件中
//os:xml文件写入流 encoding:流的编码
xs.setOutput(mContext.openFileOutput("backupsms2.xml", Context.MODE_PRIVATE), "utf-8");
//3.序列化一个xml的声明头
//encoding:xml文件的编码 standalone:是否独立
xs.startDocument("utf-8", true);
//4.序列化一个根节点的开始节点
//namespace:命名空间 name: 标签的名称
xs.startTag(null, "Smss");
//5.循环遍历list集合序列化一条条短信

for (SmsBean smsBean : allSms) {
xs.startTag(null, "Sms");
//name:属性的名称 value:属性值
xs.attribute(null, "id", smsBean.id+"");

xs.startTag(null, "num");
//写一个标签的内容
xs.text(smsBean.num);
xs.endTag(null, "num");


xs.startTag(null, "msg");
xs.text(smsBean.msg);
xs.endTag(null, "msg");


xs.startTag(null, "date");
xs.text(smsBean.date);
xs.endTag(null, "date");

xs.endTag(null, "Sms");
}

//6.序列化一个根节点的结束节点
xs.endTag(null, "Smss");
//7.将xml写入到文件中,完成xml的序列化
xs.endDocument();
return true;

}catch (Exception e) {
e.printStackTrace();
}
return false;
}

//解析xml文件读取短信内容
public static int restoreSms_android(Context mContext) {
ArrayList<SmsBean> arrayList = null;
SmsBean smsBean = null;
try{
//1.通过Xml获取一个XmlPullParser对象
XmlPullParser xpp = Xml.newPullParser();
//2.设置XmlPullParser对象的参数,需要解析的是哪个xml文件,设置一个文件读取流
//扩展:通过context获取一个资产管理者对象
//AssetManager assets = mContext.getAssets();
//InputStream inputStream = assets.open("backupsms.xml");//通过资产管理者对象能获取一个文件读取流
//xpp.setInput(inputStream,"utf-8");
xpp.setInput(mContext.openFileInput("backupsms2.xml"), "utf-8");
//3.获取当前xml行的事件类型
int type = xpp.getEventType();
//4.判断事件类型是否是文档结束的事件类型
while(type != XmlPullParser.END_DOCUMENT){
//5.如果不是,循环遍历解析每一行的数据。解析一行后,获取下一行的事件类型

String currentTagName = xpp.getName();
//判断当前行的事件类型是开始标签还是结束标签
switch (type) {
case XmlPullParser.START_TAG:
if(currentTagName.equals("Smss")){
//如果当前标签是Smss,需要初始化一个集合
arrayList = new ArrayList<SmsBean>();
}else if(currentTagName.equals("Sms")){

smsBean = new SmsBean();
smsBean.id = Integer.valueOf(xpp.getAttributeValue(null, "id"));

}else if(currentTagName.equals("num")){
smsBean.num = xpp.nextText();
}else if(currentTagName.equals("msg")){
smsBean.msg = xpp.nextText();
}else if(currentTagName.equals("date")){
smsBean.date = xpp.nextText();
}
break;
case XmlPullParser.END_TAG:
//当前结束标签是Sms的话,一条短信数据封装完成, 可以加入list中
if(currentTagName.equals("Sms")){
arrayList.add(smsBean);
}
break;
default:
break;
}

type = xpp.next();//获取下一行的事件类型
}

return arrayList.size();

}catch (Exception e) {
e.printStackTrace();
}
return 0;
}

}
六、 SmsDao类代码
package com.zgs.message.dao;

import java.util.ArrayList;

import com.zgs.message.bean.SmsBean;

public class SmsDao {

public static ArrayList<SmsBean> getAllSms() {

ArrayList<SmsBean> arrayList = new ArrayList<SmsBean>();

SmsBean smsBean = new SmsBean();
smsBean.id = 1;
smsBean.num = "12132110";
smsBean.msg = "送到附近都是浪费";
smsBean.date = "2012-08-29";
arrayList.add(smsBean);

SmsBean smsBean1 = new SmsBean();
smsBean1.id = 2;
smsBean1.num = "13232320";
smsBean1.msg = "来一个哦,哈哈";
smsBean1.date = "2019-08-21";
arrayList.add(smsBean1);

SmsBean smsBean2 = new SmsBean();
smsBean2.id = 3;
smsBean2.num = "7899349328";
smsBean2.msg = "来但是双方的首发第三方";
smsBean2.date = "2019-08-29";
arrayList.add(smsBean2);

return arrayList;

}


}
七、 SmsBean类代码
package com.zgs.message.bean;

public class SmsBean {

public String num ;
public String msg;
public String date;
public int id;

}