您的位置:首页 > 其它

2015.5.10安卓笔记之sharedprefrence、xml序列化、pull解析xml文件

2015-05-18 18:52 477 查看
1.获得sharedprefrence对象

SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE);

注意:config不需要指定后缀名,sharedprefrence会自动在包名下创建/shared_pfefs/config.xml配置文件。

2.获得编辑器,往里面存数据,存完后记得提交 .commit();

Editor editor=sp.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();

3.xml序列化(把xml文件从内存里写到文件里)

XmlSerializer serializer = Xml.newSerializer();// 获得序列化对象
File file = new File(Environment.getExternalStorageDirectory(),
"info.xml");
FileOutputStream os = new FileOutputStream(file);
serializer.setOutput(os, "utf-8");// 指定以某种格式写到一个输出流
serializer.startDocument("utf-8", true);// 头部的定义,是否为独立文件
serializer.startTag(null, "phones");

for (Phone p : list) {
serializer.startTag(null, "phone");
serializer.attribute(null, "id", p.getId() + "");

serializer.startTag(null, "number1");
serializer.text(p.getNumber());
serializer.endTag(null, "number1");

serializer.startTag(null, "time");
serializer.text(p.getTime());
serializer.endTag(null, "time");

serializer.startTag(null, "name");
serializer.text(p.getName());
serializer.endTag(null, "name");

serializer.startTag(null, "type");
serializer.text(p.getType() + "");
serializer.endTag(null, "type");

serializer.endTag(null, "phone");
}

serializer.endTag(null, "phones");
serializer.endDocument();

4.java中解析xml文件两种方式:

①DOM解析 加载内存,生成一个树状结构 缺点:消耗内存较大,不适合解析较大的xml文件.

②SAX解析 基于事件的方式,自上而下,速度快,效率高 缺点:不能倒退

5.Android中解析xml

方式:pull(类似于java中的SAX解析)

代码:

有一个a.xml文件h

public static List<WeatherInfo> getWeatherInfos(InputStream is)
throws Exception {
// new xml解析器
XmlPullParser parser = Xml.newPullParser();
// 初始化parser
parser.setInput(is, "utf-8");
int type = parser.getEventType();
List<WeatherInfo> weatherinfos = null;
WeatherInfo weatherinfo = null;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("info".equals(parser.getName())) {
weatherinfos = new ArrayList<WeatherInfo>();
} else if ("city".equals(parser.getName())) {
weatherinfo = new WeatherInfo();
String strid = parser.getAttributeValue(0);
weatherinfo.setId(Integer.parseInt(strid));
} else if ("name".equals(parser.getName())) {
System.out.println(parser.nextText());
String name = parser.nextText();
Log.i("Test",name);
weatherinfo.setName(name);
} else if ("pm".equals(parser.getName())) {
weatherinfo.setPm(parser.nextText());
} else if ("wind".equals(parser.getName())) {
weatherinfo.setWind(parser.nextText());
} else if ("weather".equals(parser.getName())) {
weatherinfo.setWeather(parser.nextText());
} else if ("temp".equals(parser.getName())) {
weatherinfo.setTemp(parser.nextText());
} else if ("date".equals(parser.getName())) {
weatherinfo.setDate(parser.nextText());
}

break;

case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
weatherinfos.add(weatherinfo);
weatherinfo = null;
}

break;

}
type = parser.next();
}

return weatherinfos;

}

MainActivity.java

//获得资源一种方式
List<WeatherInfo> infos = WeatherService
.getWeatherInfos(MainActivity.class.getClass()
.getResourceAsStream("a.xml"));
//获得资源一种方式

List<WeatherInfo> list = WeatherService
.getWeatherInfos(getResources().getAssets().open("a.xml"));
StringBuffer sb = null;

for (WeatherInfo info : list) {
String str = info.toString();
sb.append(str);
sb.append("\n");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: