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");
}
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");
}
相关文章推荐
- 安卓中使用XmlPullParser解析xml文件
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- 安卓笔记之XML解析(DOM、SAX、PULL)
- 安卓使用DOM,SAX,PULL解析xml,安卓写入xml文件
- XmlPullParser 解析xml文件
- Android下Xml解析技术(四)、pull生成Xml文件
- android使用XmlPullParser来解析XML文件
- pull解析xml学习笔记
- android菜鸟学习笔记19----Android数据存储(三)XML文件的解析及序列化
- 使用Xml类下的newPullParser ()、newSerializer ()方法,解析xml数据和序列化生成xml数据
- Android中采用XmlPullParser解析xml文件异常的问题
- XmlPullParser解析xml文件(还原短信)
- 使用pull方法解析Xml文件(省市县Xml)
- android 对xml文件的pull解析,生成xml ,对xml文件的增删
- Android中使用xml保存数据(xml文件序列化)及使用pull解析xml文件
- android-pull方式解析xml文件以及XML文件的序列化
- xml文件下载到本地—XmlPullParser解析下载到本地的xml(vlc)
- 安卓系列三(使用pull 方式解析xml文件)
- 用pull解析xml和pullParse生成xml文件详解
- ANDROID基础学习笔记_4.1_采用pull解析xml文件