加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 09:43:07 所属栏目:百科 来源:网络整理
导读:1.需要使用到的java文件 解析china.xml文件中的内容。 利用pull方式进行xml文件步骤: ①直接创建出XmlPullParser解析器对象 XmlPullParser xmlPullParser = Xml.newPullParser(); ②设置解析的文件输入流 并且制定输入流在操作方式中的编码方式 xmlPullPars

1.需要使用到的java文件

解析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://www.52php.cn/article/p-tpimvxmw-bhd.html或者http://www.52php.cn/article/p-vswgxvyc-bhd.html

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

按钮的方式结果如图:


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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读