码迷,mamicode.com
首页 > 其他好文 > 详细

利用pull方式进行xml文件查看天气

时间:2014-06-20 12:27:50      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   java   

1.需要使用到的java文件

bubuko.com,布布扣

解析china.xml文件中的内容。

利用pull方式进行xml文件步骤:

①直接创建出XmlPullParser解析器对象  XmlPullParser xmlPullParser = Xml.newPullParser();

②设置解析的文件输入流 并且制定输入流在操作方式中的编码方式    xmlPullParser.setInput(getClass().getClassLoader().getResourceAsStream("china.xml"), "UTF-8");

③获取解析文件时返回的EventType

④利用XmlPullParser中自带的START_DOCUMENT、END_DOCUMENT、START_TAG、END_TAG定值进行判断和赋值

注意:获取标签及标签中的对象在START_TAG方法中进行,而在END_TAG中即标签结束后将当前属性添加到集合中。

2.xml文件的格式有两种:

1)第一种如下:

<city
        cityname="南京"     
        pyName="jiangsu"
        quName="江苏"
        state1="1"
        state2="1"
        stateDetailed="多云"
        tem1="24"
        tem2="19"
        windState="西北风3-4级" />
    <city
        cityname="北京"     
        pyName="beijing"
        quName="北京"
        state1="1"
        state2="1"
        stateDetailed="多云"
        tem1="24"
        tem2="14"
        windState="西北风4-5级" />

2)第二种如下:

<city>
        <cityname>长沙</cityname>
        <pyName>hunan</pyName>
        <quName>湖南</quName>
        <state1>1</state1>
        <state2>1</state2>
        <stateDetailed>晴</stateDetailed>
        <tem1>23</tem1>
        <tem2>34</tem2>
        <windState>东南风2-3级</windState>
    </city>

3.第一种xml格式的进行解析时使用如下方式:

String name = xmlPullParser.getName();
if (name.equals("city")) {
	// 声明当前的city对象
	currentCity = new City();
	int count = xmlPullParser.getAttributeCount();
	// System.out.println("个数:::::"+count);
	if (count > 0) {
	// 给当前的city对象赋值
		currentCity.setCityName(xmlPullParser.getAttributeValue(null, "cityname"));
		currentCity.setPyName(xmlPullParser.getAttributeValue(null, "pyName"));
		currentCity.setQuName(xmlPullParser.getAttributeValue(null, "quName"));
		currentCity.setState1(xmlPullParser.getAttributeValue(null, "state1"));
		currentCity.setStateDetailed(xmlPullParser.getAttributeValue(null, "stateDetailed"));
		currentCity.setTem1(xmlPullParser.getAttributeValue(null, "tem1"));
		currentCity.setTem2(xmlPullParser.getAttributeValue(null, "tem2"));
		currentCity.setWindState(xmlPullParser.getAttributeValue(null, "windState"));
	}

} 

4.第二种xml格式的进行解析时使用如下方式:

if (currentCity != null) {
	if (name.equalsIgnoreCase("cityname")) {
		currentCity.setCityName(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("pyName")) {
		currentCity.setPyName(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("quName")) {
		currentCity.setQuName(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("state1")) {
		currentCity.setState1(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("state1")) {
		currentCity.setState2(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("stateDetailed")) {
		currentCity.setStateDetailed(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("tem1")) {
		currentCity.setTem1(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("tem2")) {
		currentCity.setTem2(xmlPullParser.nextText());
	} else if (name.equalsIgnoreCase("windState")) {
		currentCity.setWindState(xmlPullParser.nextText());
}

5.利用pull方式解析xml文件查看天气界面的搭建和DOM方式还有SAX方式是一样的,查看的方式也几乎相似,参照:

http://blog.csdn.net/xia09222826/article/details/28434189或者http://blog.csdn.net/xia09222826/article/details/28383941

说明:界面搭建时,可以使用RadioGroup(按钮群)或者spinner插件进行城市的选择。

按钮的方式结果如图:
bubuko.com,布布扣

利用pull方式进行xml文件相关的代码:

package www.csdn.net.xml;

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

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;
import domain.City;

public class PullXml {

	private static final String TAG = "PullXml";

	public List<City> pullXml() {
		List<City> entities = null;
		City currentCity = null;
		// 直接创建出XmlPullParser解析器对象
		XmlPullParser xmlPullParser = Xml.newPullParser();

		try {
			// 设置解析的文件输入流 并且制定输入流在操作方式中的编码方式
			xmlPullParser.setInput(getClass().getClassLoader()
					.getResourceAsStream("china.xml"), "UTF-8");
			// 获取解析文件时返回的EventType
			int eventType = xmlPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					entities = new ArrayList<>();
					Log.i(TAG, "----startdocument-----");
					break;
				case XmlPullParser.START_TAG:
					// 获取标签名
					String name = xmlPullParser.getName();
					if (name.equals("city")) {
						// 声明当前的city对象
						currentCity = new City();
						int count = xmlPullParser.getAttributeCount();
						// System.out.println("个数:::::"+count);
						if (count > 0) {
							// 给当前的city对象赋值
							currentCity.setCityName(xmlPullParser
									.getAttributeValue(null, "cityname"));
							currentCity.setPyName(xmlPullParser
									.getAttributeValue(null, "pyName"));
							currentCity.setQuName(xmlPullParser
									.getAttributeValue(null, "quName"));
							currentCity.setState1(xmlPullParser
									.getAttributeValue(null, "state1"));
							currentCity.setStateDetailed(xmlPullParser
									.getAttributeValue(null, "stateDetailed"));
							currentCity.setTem1(xmlPullParser
									.getAttributeValue(null, "tem1"));
							currentCity.setTem2(xmlPullParser
									.getAttributeValue(null, "tem2"));
							currentCity.setWindState(xmlPullParser
									.getAttributeValue(null, "windState"));
						}

					} else if (currentCity != null) {
						if (name.equalsIgnoreCase("cityname")) {
							currentCity.setCityName(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("pyName")) {
							currentCity.setPyName(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("quName")) {
							currentCity.setQuName(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("state1")) {
							currentCity.setState1(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("state1")) {
							currentCity.setState2(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("stateDetailed")) {
							currentCity.setStateDetailed(xmlPullParser
									.nextText());
						} else if (name.equalsIgnoreCase("tem1")) {
							currentCity.setTem1(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("tem2")) {
							currentCity.setTem2(xmlPullParser.nextText());
						} else if (name.equalsIgnoreCase("windState")) {
							currentCity.setWindState(xmlPullParser.nextText());
						}
					}
					//Log.i(TAG, "----starttag-----" + name);
					break;
				case XmlPullParser.END_TAG:
					//String name2 = xmlPullParser.getName();
					//标签结束时添加到集合中
					if (xmlPullParser.getName().equalsIgnoreCase("city")
							&& currentCity != null) {
						// 将当前属性添加到集合中
						entities.add(currentCity);
						currentCity= null;
					}
					//Log.i(TAG, "----endtag-----" + name2);
					break;
				}
				eventType = xmlPullParser.next();

			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return entities;
	}

}


 

 

利用pull方式进行xml文件查看天气,布布扣,bubuko.com

利用pull方式进行xml文件查看天气

标签:android   style   class   blog   code   java   

原文地址:http://blog.csdn.net/xia09222826/article/details/28441529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!