1 package com.lidaochen.phonecall; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Xml; 6 import android.view.View; 7 import android.widget.TabHost; 8 import android.widget.TextView; 9 import android.widget.ThemedSpinnerAdapter;10 11 import java.io.InputStream;12 import java.util.ArrayList;13 import java.util.List;14 15 public class MainActivity extends AppCompatActivity{16 @Override17 protected void onCreate(Bundle savedInstanceState) {18 super.onCreate(savedInstanceState);19 setContentView(R.layout.activity_main);20 try21 {22 // 找到TextView控件23 TextView textView = (TextView)findViewById(R.id.textView);24 // 通过上下文获取资产管理者25 InputStream inputStream = getAssets().open("weather.xml");26 // 调用我们定义的方法解析XML文件27 Listweatherlists = WeatherParser.parserXml(inputStream);28 StringBuffer sb = new StringBuffer();29 for (Channel channel:weatherlists)30 {31 sb.append(channel.toString());32 }33 // 将解析的文件内容显示到TextView控件上面34 textView.setText(sb.toString());35 }36 catch (Exception e)37 {38 e.printStackTrace();39 }40 }41 }
1 package com.lidaochen.phonecall; 2 3 import android.util.Xml; 4 5 import org.xmlpull.v1.XmlPullParser; 6 7 import java.io.InputStream; 8 import java.util.ArrayList; 9 import java.util.List;10 11 public class WeatherParser {12 // 服务器是以流的形式把数据返回的13 public static ListparserXml(InputStream In) throws Exception14 {15 // 声明集合对象16 List weatherlists = null;17 Channel channel = null;18 // 获取XmlPullParser解析的实例19 XmlPullParser xmlPullParser = Xml.newPullParser();20 // 设置XmlPullParser的参数21 xmlPullParser.setInput(In, "utf-8");22 // 获取事件类型23 int type = xmlPullParser.getEventType();24 while(type != XmlPullParser.END_DOCUMENT)25 {26 switch (type)27 {28 case XmlPullParser.START_TAG:29 if ("weather".equals(xmlPullParser.getName()))30 {31 // 创建一个集合对象32 weatherlists = new ArrayList ();33 }34 else if ("channel".equals(xmlPullParser.getName()))35 {36 // 创建Channel对象37 channel = new Channel();38 // 获取id值39 String id = xmlPullParser.getAttributeValue(0);40 channel.setId(id);41 }42 else if ("city".equals(xmlPullParser.getName()))43 {44 String city = xmlPullParser.nextText();45 channel.setCity(city);46 }47 else if ("temp".equals(xmlPullParser.getName()))48 {49 String temp = xmlPullParser.nextText();50 channel.setTemp(temp);51 }52 else if ("wind".equals(xmlPullParser.getName()))53 {54 String wind = xmlPullParser.nextText();55 channel.setWind(wind);56 }57 else if ("pm250".equals(xmlPullParser.getName()))58 {59 String pm250 = xmlPullParser.nextText();60 channel.setPm250(pm250);61 }62 break;63 case XmlPullParser.END_TAG: // 解析结束标志64 // 判断要解析的结束标签是不是channel65 if ("channel".equals(xmlPullParser.getName()))66 {67 // 把javabean对象存到集合中68 weatherlists.add(channel);69 }70 break;71 }72 // 不停的向下解析73 type = xmlPullParser.next();74 }75 76 return weatherlists;77 }78 }
1 package com.lidaochen.phonecall; 2 3 public class Channel { 4 private String id; 5 private String city; 6 private String temp; 7 private String wind; 8 private String pm250; 9 10 public String getId() {11 return id;12 }13 14 public void setId(String id) {15 this.id = id;16 }17 18 public String getCity() {19 return city;20 }21 22 public void setCity(String city) {23 this.city = city;24 }25 26 public String getTemp() {27 return temp;28 }29 30 public void setTemp(String temp) {31 this.temp = temp;32 }33 34 public String getWind() {35 return wind;36 }37 38 public void setWind(String wind) {39 this.wind = wind;40 }41 42 public String getPm250() {43 return pm250;44 }45 46 public void setPm250(String pm250) {47 this.pm250 = pm250;48 }49 50 @Override51 public String toString()52 {53 return "Channel [id=" + id + ", city=" + city + ", temp=" + temp54 + ", wind=" + wind + ", pm250=" + pm250 + "]\n";55 }56 }
下面是要解析的XML文件,需要放在assets目录下
1 23 4 10 11北京 525° 63 7300 8 912 18 19郑州 1320° 144 15300 16 1720 26 27长春 2110° 224 23100 24 2528 33 34 35沈阳 2920° 301 3150 32