在上一篇文章写了SAX解析XML,感觉Pull方式和SAX方式非常相似,只是SAX需要一个辅助的类,解析时触发事件后在回调方法里面写代码,而Pull则通过调用事件类型的方法来返回一个int值,然后根据这个值来写代码。
首先自定义一个XML文件:Student.xml,注意是新建file而不是xml。
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <students> <student id="1"> <name>张三</name> <sex>男</sex> <age>18</age> </student> <student id="2"> <name>李四</name> <sex>女</sex> <age>19</age> </student> <student id="3"> <name>王五</name> <sex>男</sex> <age>20</age> </student> </students> </span>
以解析第一项数据为例:
<pre name="code" class="java"><span style="font-size:14px;"><students> <student id="1"> <name>张三</name> <sex>男</sex> <age>18</age> </student> </span>
最后在activity里面通过一个Button来解析数据,其中用到几个类。
<span style="font-size:14px;">package com.example.xml_sax_demo_1; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserFactory; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Xml; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub try { readXML(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } private void readXML() throws Exception { Student student = null;List<Student> list = null; XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance(); XmlPullParser parser = xmlPullParserFactory.newPullParser(); //实例化XmlPullParser InputStream stream = this.getClass().getClassLoader() .getResourceAsStream("Student.xml"); // 获得输入流 parser.setInput(stream, "UTF-8"); //两个参数输入流,编码格式 int event = parser.getEventType();// 产生第一个事件 while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Student>(); //文档开始实例化list break; case XmlPullParser.START_TAG: if ("student".equals(parser.getName())) { student = new Student(); student.setId(Integer.parseInt(parser.getAttributeValue(0))); } if (student != null) { if ("name".equals(parser.getName())) { student.setName(parser.nextText()); } else if ("sex".equals(parser.getName())) { student.setSex(parser.nextText()); } else if ("age".equals(parser.getName())) { student.setAge(Integer.parseInt(parser.nextText())); } } break; case XmlPullParser.END_TAG: if ("student".equals(parser.getName())) { list.add(student); student = null; } break; } event = parser.next();// 进入下一个元素并触发相应事件 } for (Student stu : list) { System.out.println(stu.toString()); } } }</span>
结果:
原文地址:http://blog.csdn.net/leelit/article/details/39029435