XmlSerializer的使用

xml的序列化生成器

try {
    		//声明xml序列化
			XmlSerializer xs = Xml.newSerializer();
			//得到sdcard路径
			String strPath = Environment.getExternalStorageDirectory().getPath();
			//创建xml文件
			File file = new File(strPath, "xml2.xml");
			System.out.println(strPath);
			FileOutputStream fos = new FileOutputStream(file);
			//初始化序列化器,设置生成的xml文件的编码格式
			xs.setOutput(fos, "utf-8");
			//开始文档,xml文件第一行中的那个编码格式
			xs.startDocument("utf-8", true);
			//开始标签
			xs.startTag(null, "ssms");
			for (Sms sms : smsList) {
				xs.startTag(null, "body");
				xs.text(sms.getStrBody());
				xs.endTag(null, "body");
				xs.startTag(null, "address");
				xs.text(sms.getStrAddress());
				xs.endTag(null, "address");
				xs.startTag(null, "date");
				xs.text(sms.getStrDate());
				xs.endTag(null, "date");
			}
			
			xs.endTag(null, "ssms");
			//执行完这句才真正保存
			xs.endDocument();
			Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


你可能感兴趣的:(XMLSerializer)