Android Xml的解析
来源:互联网 发布:c语言打开文件目录 编辑:程序博客网 时间:2024/05/16 12:37
要解析的Xml
<?xml version="1.0" encoding="utf-8"?><weather> <channel id ='1'> <city>北京</city> <temp>25°</temp> <wind>3</wind> <pm250>300</pm250></channel> <channel id ='2'> <city>郑州</city> <temp>20°</temp> <wind>4</wind> <pm250>300</pm250></channel><channel id ='3'> <city>长春</city> <temp>10°</temp> <wind>4</wind> <pm250>100</pm250></channel><channel id ='4'> <city>沈阳</city> <temp>20°</temp> <wind>1</wind> <pm250>50</pm250></channel></weather>解析用到的JavaBean
package com.example.a19_xml_parse;public class Channel { public String temp; public String city; public String id; public String pm250; public String wind; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPm250() { return pm250; } public void setPm250(String pm250) { this.pm250 = pm250; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } @Override public String toString() { return "Channel [id=" + id +", city=" + city + ", temp=" + temp + ", wind=" + wind + ", pm250=" +pm250 + "]"; }}解析者:
package com.example.a19_xml_parse;import android.util.Xml;import org.xmlpull.v1.XmlPullParser;import java.io.InputStream;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017/4/14. */public class WeatherParser { /** * 服务器是以流的形式把数据返回的 * @return */ public static List<Channel> parserXml(InputStream in) throws Exception { //0 声明集合对象 List<Channel> weatherLists = null; Channel channel = null; //1 获取XmlPullParser 解析的实例 XmlPullParser parser = Xml.newPullParser(); //2 设置XmlPullParser的参数 parser.setInput(in, "utf-8"); //3 获取事件类型 int type = parser.getEventType(); while (type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_TAG: //解析开始标签 //4 具体判断一下 解析到哪个开始标志 if ("weather".equals(parser.getName())) { //5 创建一个集合对象 weatherLists = new ArrayList<Channel>(); } else if ("channel".equals(parser.getName())) { //6 创建channel对象 channel = new Channel(); //7 获取id值 String id = parser.getAttributeValue(0); channel.setId(id); } else if ("city".equals(parser.getName())) { //8 获取city的数据 String city = parser.nextText(); channel.setCity(city); } else if ("temp".equals(parser.getName())) { String temp = parser.nextText(); channel.setTemp(temp); } else if ("wind".equals(parser.getName())) { String wind = parser.nextText(); channel.setWind(wind); } else if ("pm250".equals(parser.getName())) { String pm250 = parser.nextText(); channel.setPm250(pm250); } break; case XmlPullParser.END_TAG: //解析结束标签 //判断要解析的结束标签 if ("channel".equals(parser.getName())) { //把javabean对象存到集合中 weatherLists.add(channel); } break; } //不停的向下解析 type = parser.next(); } return weatherLists; }}MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { //1 显示天气信息 TextView tv_weather = (TextView) findViewById(R.id.tv_weather); //1.1 通过上下文获取资产的管理者 InputStream inputStream = getAssets().open("weather.xml"); //2 调用我们定义的解析xml业务方法 List<Channel> weatherLists = WeatherParser.parserXml(inputStream); StringBuffer sb = new StringBuffer(); for (Channel channel : weatherLists) { sb.append(channel.toString()); } //3 把数据显示到textview上 tv_weather.setText(sb.toString()); } catch (Exception e) { e.printStackTrace(); } }}StringBuffer线程安全,StringBuilder线程不安全
0 0
- android的xml解析
- android XML的解析
- android xml 的解析.
- Android的XML解析
- Android xml的解析
- android xml 的解析
- Android Xml的解析
- android关于xml的解析
- 六、android的xml解析
- android xml的pull解析
- 实用的android xml解析
- android xml文件的解析
- Android中XML的解析
- Android对XML的解析
- android解析xml的方式
- Android的sax解析xml
- Android xml数据的解析
- android中XML的解析
- 炒蘑菇
- 百度 echarts 使用总结
- Fragment中传递参数推荐用Fragment.setArguments(Bundle bundle),而不用构造方法。
- VBA入门--变量的基础内容和知识点
- HashMap解决hash冲突的方法
- Android Xml的解析
- 自旋锁、互斥锁和信号量的区别
- Golang RPC性能测试
- 静态初始板块,构造方法和初始板块它们之间的执行优先级
- 模拟数字式时钟
- Linux(CentOS)下,下载安装Nginx并配置
- js给网页中所有p元素添加onclick事件
- hdu 3549 网络流 模板题
- 1102. Invert a Binary Tree (25)