…
/**
* name 会帮助我们生成一个html文件
* mode 模式
*/
SharedPreferences sp; //可将其定义为成员变量 private SharedPreferences sp;
sp = getSharedPreferences("config", 0);
Editor edit = sp.edit();
//各种类型Int,String,Boolean,Float,Long等
edit.putString("name", name);
edit.putString("pwd", pwd);
edit.putInt("number", number);
edit.commit();
//在生成的xml文件中读取用户保存密码的文件,回显
/**
* key 与set保持一致
* defValue 默认值 ""空字符串
*/
//提取数据所用的类型要和第三步骤存储保持一致
String name = sp.getString("name", "");
String pwd2 = sp.getString("pwd", "");
StringBuffer sb = new StringBuffer();
//[1]组拼xml文件头 \" 为转义
sb.append("");
// [3]开始组拼xml根节点
sb.append("" );
// [4]开始组拼xml节点,循环从List里面取数据
for (Sms sms : smsLists) {
sb.append("" );
// [5]开始组拼电话号码节点
sb.append("" );
sb.append(sms.getNumber());
sb.append("");
// [6]开始组拼短信内容节点
sb.append("" );
sb.append(sms.getContent());
sb.append("");
// [7]开始组拼日期节点
sb.append("" );
sb.append(sms.getDate());
sb.append("");
sb.append("");
}
sb.append("");
/*getCacheDir();getFilesDir();Environment.getExternalStorageDirectory()
其中本例中还需配置权限:WRITE_EXTERNAL_STORAGE*/
File file = new File(Environment.getExternalStorageDirectory(),"xx.xml");
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());//sb
fos.close();
XmlSerializer serializer = Xml.newSerializer(); //XmlSerializer为接口
/* os:流
* encoding:编码方式
*/
serializer.setOutput(fos, "utf-8");
//[1]写xml文档的开头
/* encoding:编码方式
* standalone:生成的xml是否独立
*/
serializer.startDocument("utf-8", true);
//[2]开始写xml文档的根节点
/* namespace:命名空间
* name:节点名
*/
serializer.startTag(null, "sms");
//[3]循环写xml节点
for (Sms sms : smsLists) {
serializer.startTag(null, "sms");
serializer.startTag(null, "number");
serializer.text(sms.getNumber());
serializer.endTag(null, "number");
serializer.startTag(null, "content");
serializer.text(sms.getContent());
serializer.endTag(null, "content");
serializer.startTag(null, "date");
serializer.text(sms.getDate());
serializer.endTag(null, "date");
serializer.endTag(null, "sms");
}
serializer.endTag(null, "sms");
serializer.endDocument();//关闭刷新
fos.close();//关闭流
XmlPullParser parser = Xml.newPullParser(); //XmlPullParser为接口
parser.setInput(in, "utf-8");
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG://解析开始标签
//具体判断一下,解析到的是哪个开始标签
if("weather".equals(parser.getName())){
//创建一个集合对象
weatherLists = new ArrayList<WeatherInfo>();
}else if("channel".equals(parser.getName())){
//创建WeatherInfo对象
weatherInfo = new WeatherInfo();
//获取id值
String id = parser.getAttributeValue(0);
weatherInfo.setId(id);
}else if("city".equals(parser.getName())){
//获取city数据
String city = parser.nextText();
weatherInfo.setCity(city);
}else if("wea".equals(parser.getName())){
//获取wea数据
String wea = parser.nextText();
weatherInfo.setWea(wea);
}else if("temp".equals(parser.getName())){
//获取temp数据
String temp = parser.nextText();
weatherInfo.setTemp(temp);
}else if("wind".equals(parser.getName())){
//获取wind数据
String wind = parser.nextText();
weatherInfo.setWind(wind);
}else if("info".equals(parser.getName())){
//获取info数据
String info = parser.nextText();
weatherInfo.setInfo(info);
}
break;
case XmlPullParser.END_TAG://解析结束标签
//具体判断一下,解析到的是哪个结束标签
if("channel".equals(parser.getName())){
//把javaBean对象存到集合中
weatherLists.add(weatherInfo);
}
break;
}
//继续向下解析
type = parser.next();
}
<?xml version="1.0" encoding="utf-8"?>
<weather>
<channel id='1'>
<city>池州</city>
<wea>下雨</wea>
<temp>20℃</temp>
<wind>3</wind>
<info>请穿个长袖</info>
</channel>
</weather>
/*
上下对应着看
*/
public static final int START_DOCUMENT = 0;
public static final int END_DOCUMENT = 1;
public static final int START_TAG = 2;
public static final int END_TAG = 3;
public static final int TEXT = 4;
parser.getName(); //获取<>中的内容
parser.next(); //指向下一个节点
parser.nextText(); //获取文本内容