XmlPullParser解析xml文件(还原短信)

前端之家收集整理的这篇文章主要介绍了XmlPullParser解析xml文件(还原短信)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
/**
	 * 还原短信
	 */
	public static void retoreSms(Context context) {
		ContentResolver resolver = context.getContentResolver();
		Uri uri = Uri.parse("content://sms");

		File file = new File(Environment.getExternalStorageDirectory(),"backup.xml");
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(file));
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(bis,"utf-8");

			int event = parser.getEventType();
			SmsInfo smsInfo = null;
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("sms".equals(parser.getName())) {
						smsInfo = new SmsInfo();
					}else if ("address".equals(parser.getName())) {
						smsInfo.setAddress(parser.nextText());
					}else if("date".equals(parser.getName())){
						smsInfo.setDate(parser.nextText());
					}else if("type".equals(parser.getName())){
						smsInfo.setType(parser.nextText());
					}else if("body".equals(parser.getName())){
						smsInfo.setBody(parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					if ("sms".equals(parser.getName())) {//一条短信结束
						ContentValues values = new ContentValues();
						values.put("address",smsInfo.getAddress());
						values.put("date",smsInfo.getDate());
						values.put("type",smsInfo.getType());
						values.put("body",smsInfo.getBody());
						resolver.insert(uri,values);
					}
					break;
				}
				event = parser.next();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// can't reach
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
原文链接:https://www.f2er.com/xml/295805.html

猜你在找的XML相关文章