- xml文件的Pull解析(用type判断,parser解析获取数据)
int type = XmlPullParser.getEventType();
backup.xml
<?xml version="1.0" encoding="utf-8"?> //START_DOCUMENT
<smss>//START_TAG 判断START_TAG,一般在此String tagName = XmlPullParser.getName();
<sms> //START_TAG 一般在此new对象
<body>woshi body</body>
<address>woshi address</address>
<type>woshi type</type>
<date>woshi date</date>
</sms> //END_TAG 一般在此把对象塞进集合,对象置null
</smss> //END_DOCUMENT
示例代码:
/**
* 解析xml文件的服务类
*/
public class SmsService {
private List<SmsInfo> infos;
private Context context;
public SmsService(Context context){
this.context = context;
}
public List<SmsInfo> getSmsInfo(Context context,String fileName) {
InputStream is;
try {
infos = new ArrayList<SmsInfo>();
SmsInfo info = null;
is = context.getAssets().open(fileName);
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
if (event == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if ("sms".equals(tagName)) {
info = new SmsInfo();
} else if ("body".equals(tagName)) {
String body = parser.nextText();
info.setBody(body);
} else if ("address".equals(tagName)) {
String address = parser.nextText();
info.setAddress(address);
} else if ("type".equals(tagName)) {
String type = parser.nextText();
info.setType(type);
} else if ("date".equals(tagName)) {
String date = parser.nextText();
info.setDate(date);
}
}
if (event == XmlPullParser.END_TAG) {
if ("sms".equals(parser.getName())) {
infos.add(info);
info = null;
}
}
event = parser.next();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return infos;
}
}
/**
* 获取数据
*/
private TextView tv_content;
tv_content = (TextView)this.findViewById(R.id.tv_content);
SmsService service = new SmsService(getApplicationContext());
List<SmsInfo> infos = service.getSmsInfo(getApplicationContext(), "backup.xml");
StringBuffer sb = new StringBuffer();
for(SmsInfo info:infos){
String body = info.getBody();
String address = info.getAddress();
String date = info.getDate();
String type = info.getType();
sb.append(body+" "+address+" "+date+" "+type);
}
tv_content.setText(sb.toString());
}
- xml文件的序列化
权限配置:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
public void serializerXml(Context context){
try {
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.parse("content://sms/");
File file = new File(Environment.getExternalStorageDirectory(), "smsInfo.xml");
FileOutputStream fos;
fos = new FileOutputStream(file);
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(fos, "utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "smss");
Cursor cursor = resolver.query(uri, new String[]{"body","address","type","date"}, null, null, null);
while(cursor.moveToNext()){
String body = cursor.getString(0);
String address = cursor.getString(1);
String type = cursor.getString(2);
String date = cursor.getString(3);
serializer.startTag(null, "sms");
serializer.startTag(null, "body");
serializer.text(body);
serializer.endTag(null, "body");
serializer.startTag(null, "address");
serializer.text(address);
serializer.endTag(null, "address");
serializer.startTag(null, "type");
serializer.text(type);
serializer.endTag(null, "type");
serializer.startTag(null, "date");
serializer.text(date);
serializer.endTag(null, "date");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "smss");
serializer.endDocument();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}