1编写布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="解析"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="@string/hello_world" />
</LinearLayout>
2编写xml文件 这里只是说明怎么解析 因此没有从网络获取
<?xml version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20℃/30℃</temp>
<weather>5月20日 多云转阴</weather>
<wind>南风三到四级</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id="2">
<temp>20℃/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>
文件保存在assert文件夹下
3根据xml文件写一个weatherBean
package com.example.xmljiexi.bean;
public class WeatherBean {
private int id;
private String temp;
private String weather;
private String wind;
private String name;
private int pm;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPm() {
return pm;
}
public void setPm(int pm) {
this.pm = pm;
}
@Override
public String toString() {
return "WeatherBean [id=" + id + ", temp=" + temp + ", weather="
+ weather + ", wind=" + wind + ", name=" + name + ", pm=" + pm
+ "]";
}
}
4写一个工具类
package com.example.xmljiexi.tool;
import java.io.IOException;
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.xmljiexi.bean.WeatherBean;
public class Parser {
// 返回一个集合 集合里是每一个城市的情况
// 需要一个参数 把xml文件转化成流
public static List<WeatherBean> getParse(InputStream is)
throws XmlPullParserException, IOException {
List<WeatherBean> list = null;
WeatherBean weatherBean = null;
// 初始化
XmlPullParser parser = Xml.newPullParser();
// 设置信息所在文件流和编码
parser.setInput(is, "utf-8");
// 获取事件类型 文档开始--0 文档结束--1 标签开始--2 标签结束--3 text标签外的内容如空格 回车 等--4
int type = parser.getEventType();
// 判断文档是否结束读取
while (type != XmlPullParser.END_DOCUMENT) {
// 判断事件类型
switch (type) {
case XmlPullParser.START_DOCUMENT:
// 不作任何处理
break;
// 事件为标签开始
case XmlPullParser.START_TAG:
// 获取当前事件对应的名称
if ("infos".equals(parser.getName())) {
//进入文档,即将获得具体城市信息
list = new ArrayList<WeatherBean>();
} else if ("city".equals(parser.getName())) {
weatherBean =new WeatherBean();
String id = parser.getAttributeValue(0);
weatherBean.setId(Integer.parseInt(id));
} else if ("temp".equals(parser.getName())) {
String temp = parser.nextText();
weatherBean.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
String weather = parser.nextText();
weatherBean.setWeather(weather);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
weatherBean.setWind(wind);
} else if ("pm".equals(parser.getName())) {
String pm = parser.nextText();
weatherBean.setPm(Integer.parseInt(pm));
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
weatherBean.setName(name);
}
break;
case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
//一个城市信息解析完毕
list.add(weatherBean);
weatherBean = null;
}
break;
default:
break;
}
type = parser.next();
}
return list;
}
}
5主界面
package com.example.xmljiexi;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.example.xmljiexi.bean.WeatherBean;
import com.example.xmljiexi.tool.Parser;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView;
AssetManager assetManager;
List<WeatherBean> list;
StringBuffer stringBuffer =new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView =(TextView) findViewById(R.id.text);
assetManager = getAssets();
}
public void click(View view) {
//step1 获取asserts文件中chj.xml内容 要转化成流
try {
InputStream is = assetManager.open("chj.xml");
list = Parser.getParse(is);
for(int i = 0; i<list.size() ; i++){
WeatherBean weatherBean = list.get(i);
stringBuffer.append(weatherBean.toString()+"\n");
}
textView.setText(stringBuffer);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}