首页 > 代码库 > 通过Pull方式解析xml文件

通过Pull方式解析xml文件

说明:此案例使用的是通过Sax方式解析xml文件这篇文章里的布局文件、City类和china.xml文件(此文件内包含两种格式),所以只需要完成MainActivity和PullXml就行了,在于展示如何使用Pull方式解析xml文件。

1. PullXml类的主要代码如下:

<span style="font-size:14px;">public class 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:
					Log.i(TAG, "---START_DOCUMENT---");
					entities = new ArrayList<>();
					break;

				case XmlPullParser.START_TAG:
					// 获取标签名
					String name = xmlPullParser.getName();
					if (name.equals("city")) {
						currentCity = new City();
						int count = xmlPullParser.getAttributeCount();
						if (count > 0) {
							// 根据属性赋值
							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.setState2(xmlPullParser
									.getAttributeValue(null, "state2"));
							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("state2")) {
							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());
						}
					}
					break;

				case XmlPullParser.END_TAG:
					// 在标签结束时添加到集合中
					if (xmlPullParser.getName().equalsIgnoreCase("city")
							&& currentCity != null) {
						entities.add(currentCity);
						currentCity = null;
					}
					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;
	}

}</span>
2. MainActivity的主要步骤是:获取控件对象,实现OnItemSelectedListener接口注册监听事件,获取字符串数组,设置控件的默认值,获取解析的xml文件等,主要代码如下:

public class MainActivity extends Activity implements OnItemSelectedListener{

	private Spinner sp_cities;
	private TextView tv_fengli;
	
	private String cities[];

	private List<City> entities;
	
	private PullXml pullXml;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sp_cities = (Spinner) findViewById(R.id.sp_cities);
		tv_fengli = (TextView) findViewById(R.id.tv_fengli);
		// 注册事件
		sp_cities.setOnItemSelectedListener(this);
		// 获取字符串数组
		cities = getResources().getStringArray(R.array.cities);

		pullXml = new PullXml();
		
		// 设置控件的默认值
		sp_cities.setSelection(2);
		// 获取解析的xml文件
		entities = pullXml.pullXml();
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		for (City c : entities) {
			// 找到对应的city对象
			if (c.getQuName().equals(cities[position])) {
				// 设置风力
				tv_fengli.setText(c.getWindState());
				break;
			}
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		
	}
}
3. 主要功能:

通过点击下拉列表里的城市进行切换,实现各城市风力的显示。