歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 中使用Pull解析XML文件

Android 中使用Pull解析XML文件

日期:2017/3/1 9:39:53   编辑:Linux編程

解析XML文件是非常常用的功能,在Android客戶端中,經常與服務器通信都需要xml文件的支持,我們這裡介紹一個簡單的xml文件的解析,就是使用android中的pull方法進行解析。在java中,有dom解析和sax解析,這個pull解析有些類似於sax解析,他也是一行一行的讀取然後解析內容的方法。

首先看一下這個簡單的xml文件

<?xml version="1.0" encoding="utf-8"?>

<infos>

<city id="1">
<temp>-1℃/5℃</temp>
<weather>多雲</weather>
<wind>南風3-4級</wind>
<name>上海</name>
<pm>200</pm>
</city>

<city id="2">
<temp>-1℃/5℃</temp>
<weather>多雲</weather>
<wind>南風3-4級</wind>
<name>北京7-8</name>
<pm>800</pm>
</city>


<city id="3">
<temp>-7℃/5℃</temp>
<weather>多雲</weather>
<wind>南風3-4級</wind>
<name>哈爾濱</name>
<pm>100</pm>
</city>

</infos>

然後我們直接解析這個xml文件,在textview中顯示一下

這裡是代碼,首先是業務Bean

package com.linuxidc.weather;

public class WeatherBean {

private int id;
private String name;
private String wind;
private String weather;
private String temp;
private String pm;

@Override
public String toString() {
return "WeatherBean [id=" + id + ", name=" + name + ", wind=" + wind
+ ", weather=" + weather + ", temp=" + temp + ", pm=" + pm
+ "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

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;
}
}

然後是解析xml文件的主要代碼

package com.linuxidc.weather;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class ParseXml {

public static List<WeatherBean> parse(InputStream is) {
List<WeatherBean> list = null;
WeatherBean bean = null;
try {
XmlPullParser parser = Xml.newPullParser();
// 初始化解析器
parser.setInput(is, "utf-8");

int type = parser.next();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("infos".equals(parser.getName())) {
list = new ArrayList<WeatherBean>();
} else if ("city".equals(parser.getName())) {
bean = new WeatherBean();
bean.setId(Integer.valueOf(parser.getAttributeValue(0)));
} else if ("temp".equals(parser.getName())) {
String temp = parser.nextText();
bean.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
String weather = parser.nextText();
bean.setWeather(weather);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
bean.setWind(wind);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
bean.setName(name);
} else if ("pm".equals(parser.getName())) {
String pm = parser.nextText();
bean.setPm(pm);
}
break;

case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
// 一個城市的信息處理完畢
list.add(bean);
bean = null;
}
break;
}

type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}

最後在mainactivity中使用這個代碼,使用類加載器完成這個簡單的功能

package com.linuxidc.weather;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

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);
List<WeatherBean> list = ParseXml.parse(MainActivity.class.getClassLoader().getResourceAsStream("test.xml"));
StringBuffer sb = new StringBuffer();
for(WeatherBean bean : list){
String str = bean.toString();
sb.append(str);
sb.append("\n");
}

tv.setText(sb.toString());
}


}

這樣看來,解析xml文件還是非常簡單的。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved