[置顶]xml文件解析方式详解、 pull方式解析xml文件实现代码
2015-09-10 09:36
711 查看
解析xml文件
DOM 解析:把文件一次性加载到内存里,生成一个树状结构,在内存中对树状节点进行修改或添加节点
这种方法的缺点:
消耗的内存太大
SAX解析:基于事件方式解析,每产生一个数据就得到该事件,但是事件是由上到下,不能回退
特点:速度快,效率高,不能回退
android中还有一种解析:pull解析
特点:类似于SAX解析,是基于事件的,语法方便理解
循环中要加上循环终止的条件的变化语句:type = parser.next()
解析完所有变量之后把常量置为null,节省内存,方便垃圾清理器回收对象
pull方式解析xml文件实现代码
xml文件
xml对应的实体bean文件
pull解析代码
[/code]
MainActivity文件
效果图:
需求:
读取xml文件中的信息展现到界面上,把获取信息的操作放到一个业务bean类里面实现步骤:
建立xml文件,粘贴到项目的src目录下,用类加载器去读取xml文件的内容.几种xml文件解析方式:
java web中有2中解析方式DOM 解析:把文件一次性加载到内存里,生成一个树状结构,在内存中对树状节点进行修改或添加节点
这种方法的缺点:
消耗的内存太大
SAX解析:基于事件方式解析,每产生一个数据就得到该事件,但是事件是由上到下,不能回退
特点:速度快,效率高,不能回退
android中还有一种解析:pull解析
特点:类似于SAX解析,是基于事件的,语法方便理解
pull解析的原理:
一个一个标签(tag)的去解析,至上而下解析到文件的末尾,因为xml文件里面的格式相似度高,可以用到while循环,没有解析到文件的末尾之前一直解析循环中要加上循环终止的条件的变化语句:type = parser.next()
解析完所有变量之后把常量置为null,节省内存,方便垃圾清理器回收对象
pull方式解析xml文件实现代码
xml文件
<span style="font-size:18px;"> <?xml version="1.0" encoding="utf-8"?> <infos> <city id="1"> <temp>20摄氏度/30摄氏度</temp> <weather>5月20日 多云转阴</weather> <wind>南风3-4级</wind> <name>上海</name> <pm>200</pm> </city> <city id="2"> <temp>26摄氏度/30摄氏度</temp> <weather>5月20日 多云转阴</weather> <wind>南风7-8级</wind> <name>北京</name> <pm>800</pm> </city> <city id="3"> <temp>6摄氏度/10摄氏度</temp> <weather>5月20日 多云转阴</weather> <wind>南风7-8级</wind> <name>哈尔滨</name> <pm>100</pm> </city> </infos></span>
xml对应的实体bean文件
<span style="font-size:18px;"> package com.example.weather.domain; import java.io.InputStream; import java.util.List; //业务bin要放在domain目录下 public class WeatherInfos { private int id; private String wind; private String weather; private String temp; private String pm; private String name; /** * get、set方法 * @return */ public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getPm() { return pm; } public void setPm(String pm) { this.pm = pm; } @Override public String toString() { return "WeatherInfos [城市id=" + id + ", 风力=" + wind + ", 天气状况=" + weather + ", 温度=" + temp + ", pm2.5=" + pm + ", 名称=" + name + "]"; } }</span>
pull解析代码
<span style="font-size:18px;"> package com.example.weather.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Xml; import com.example.weather.domain.WeatherInfos; public class WeatherService { //返回天气信息的集合,没有使用任何类的变量,可以将其定义为static public static List<WeatherInfos> getWeatherInfos(InputStream is) throws Exception { //步骤1:new一个解析器出来 XmlPullParser parser = Xml.newPullParser(); //步骤2:初始化解析器 parser.setInput(is,"utf-8"); List<WeatherInfos> weatherInfos = null; WeatherInfos weatherInfo = null; int type = parser.getEventType(); while(type != XmlPullParser.END_DOCUMENT) { switch(type) { case XmlPullParser.START_TAG: if("infos".equals(parser.getName())) { //解析到全局标签的开始 weatherInfos = new ArrayList<WeatherInfos>();//声明一个weather对象 } else if("city".equals(parser.getName())) { weatherInfo = new WeatherInfos(); String idStr = parser.getAttributeValue(0); weatherInfo.setId(Integer.parseInt(idStr)); } else if("temp".equals(parser.getName())) { String temp =parser.nextText();//nextText()得到两个标签之间的文本信息 weatherInfo.setTemp(temp);//得到状态信息之后,把里面的文本塞进来 } else if("weather".equals(parser.getName())) { String weather =parser.nextText();//nextText()得到两个标签之间的文本信息 weatherInfo.setWeather(weather);//得到温度信息之后,把里面的文本塞进来 } else if("wind".equals(parser.getName())) { String wind =parser.nextText();//nextText()得到两个标签之间的文本信息 weatherInfo.setWind(wind);//得到风力信息之后,把里面的文本塞进来 } else if("name".equals(parser.getName())) { String name =parser.nextText();//nextText()得到两个标签之间的文本信息 weatherInfo.setName(name);//得到城市名称信息之后,把里面的文本塞进来 } else if("pm".equals(parser.getName())) { String pm =parser.nextText();//nextText()得到两个标签之间的文本信息 weatherInfo.setPm(pm);//得到pm信息之后,把里面的文本塞进来 } break; case XmlPullParser.END_TAG: if("city".equals(parser.getName())) { //一个城市的信息已经处理完毕了 weatherInfos.add(weatherInfo);//把城市信息加到信息集合里面 weatherInfo = null;//清空数据,便于回收变量 } break; } type = parser.next(); } return weatherInfos;//返回集合信息 } }</span>
[/code]
MainActivity文件
<span style="font-size:18px;"> package com.example.weather; import java.util.List; import com.example.weather.domain.WeatherInfos; import com.example.weather.service.WeatherService; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView)findViewById(R.id.tv); //输入参数:天气信息的输入流,这里可以通过类加载器的方式加载在src目录下的weather.xml文件 try { List<WeatherInfos> infos = WeatherService.getWeatherInfos(MainActivity.class.getClassLoader() .getResourceAsStream("weather.xml")); StringBuffer sb = new StringBuffer(); //java中for循环书写方式:for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体 //在infos里面进行循环 for(WeatherInfos info : infos) { String str = info.toString(); sb.append(str); sb.append("\n"); } tv.setText(sb.toString()); } catch (Exception e) { //makeText()的第3个参数是时长 Toast.makeText(this, "解析文件失败", 0).show(); e.printStackTrace(); } } }</span>
效果图:
相关文章推荐
- [置顶]打log的方式检查程序里面的问题 及示例代码 详解
- [置顶]eclipse android实用开发快捷键使用总结
- [置顶]编程规范
- 如何获取最新的代码?
- 在Eclipse中导入Tiny工程,有下面的错误,是什么原因?
- 开源中国GIT中Java分类下TOP10项目的活动情况分析
- 《开源框架那点事儿16》:缓存相关代码的演变
- yii2 验证码详细代码
- 零基础学python-14.2 python的文档资源:文档字符串
- 零基础学python-14.2 python的文档资源:文档字符串
- java实现遍历Map的方法
- java中的==与equals()
- java基础学习总结-----线程(一)
- 在VS2010下,用C语言编写pthread多线程程序的配置
- java帮助文档系列JDK1.5 JDK1.6 JDK1.7官方中英完整版下载
- 将php和mysql加入环境变量
- myeclipse10激活注册码生成器代码
- 分析setting源代码获取sd卡大小
- JAVA中正则表达式匹配,替换,查找,切割的方法
- 拼接字符串代码段