利用pull方式进行xml文件查看天气
来源:互联网 发布:java 时间控件 编辑:程序博客网 时间:2024/06/08 23:53
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://blog.csdn.net/xia09222826/article/details/28434189或者http://blog.csdn.net/xia09222826/article/details/28383941
说明:界面搭建时,可以使用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");// 获取解析文件时返回的EventTypeint 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return entities;}}
- 利用pull方式进行xml文件查看天气
- DOM方式解析xml文件查看天气
- SAX方式解析xml文件查看天气
- 利用PULL解析方式解析XML文件
- 利用Dom,Sax,Pull三种方式解析xml文件
- 利用pull解析xml文件
- pull 方式解析xml文件
- Pull方式解析XML文件
- PULL方式解析XML文件
- Android利用Pull解析器对List进行解析存储为XML文件(二)
- 利用pull解析器解析xml文件
- android中利用pull解析xml文件
- 利用Pull生成和解析xml文件
- Pull进行 xml 文件的解析
- android Pull方式解析xml文件
- Android pull方式解析XML文件
- android 使用pull方式解析xml文件
- XML文件解析(Pull方式)
- java连接sql数据库代码
- Android 与 iOS 持久化 Http Cookie
- 深拷贝详解
- C++构造函数详解
- 源码,反码,补码,二进制 反码求和
- 利用pull方式进行xml文件查看天气
- 对elf文件连接的理解(一)-重定位概述
- OpenCV Machine Learning 之 K最近邻分类器 K-Nearest Neighbors
- MFC hook注入dll,远程线程注入dll
- 一种经典的网络游戏服务器架构
- WPF Control event 触发
- xml基础训练
- 歧路亡羊
- poj 3167 Cow Patterns(kmp)