Pull解析器主要的事件和方法 XmlPullParser parser = Xml.newPullParser(); //获得pull解析器 parser.setInput(fileInputStream, “utf-8”); //把xml文档以流的形式传给解析器 int eventCode = parser.getEventType(); //获得解析事件返回的code XmlPullParser.START_DOCUMENT //开始文档 XmlPullParser.END_DOCUMENT //结束文档 XmlPullParser.START_TAG //开始节点 XmlPullParser.END_TAG //结束节点 eventCode = parser.next(); //解析下一个节点 parser.getName() //获得节点名称 parser.getAttributeValue(null, “id”)//获得节点属性 parser.nextText()//获得test文本值
在assets下面新建一个student.xml
XML_PullActivity.java
package rw.pull; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class XML_PullActivity extends Activity { /** Called when the activity is first created. */ private Button button; private ListView listView; private Student student; private List<Student> lisStudents=new ArrayList<Student>(); private List<String> list=new ArrayList<String>(); private String Result=""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button=(Button) findViewById(R.id.button1); button.setOnClickListener(new ButtonListener()); listView=(ListView)findViewById(R.id.listView1); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub try { AssetManager assetManager=getAssets(); InputStream inputStream=assetManager.open("student.xml"); XmlPullParserFactory factory=XmlPullParserFactory.newInstance(); XmlPullParser parser=factory.newPullParser(); parser.setInput(inputStream, "UTF-8"); int EventType=parser.getEventType(); while (XmlPullParser.END_DOCUMENT!=EventType) { String string=parser.getName(); //Log.i("----->", "运行到这里了"); //Log.i("---------",string); switch (EventType) { case XmlPullParser.START_TAG: if (string.equals("student")) { student=new Student(); student.setId(Long.parseLong(parser.getAttributeValue(0))); Log.i("---->", parser.getAttributeValue(0)); }if (string.equals("name")) { student.setName(parser.nextText()); //Log.i("--------->", parser.nextText()); }if (string.equals("speciality")) { student.setSpeciality(parser.nextText()); }if (string.equals("qq")) { student.setQQ(Long.parseLong(parser.nextText())); } break; case XmlPullParser.END_TAG: if (string.equals("student")&&string!=null) { lisStudents.add(student); student=null; } break; default: break; } EventType =parser.next(); } for (Student stu : lisStudents) { Result=(stu.getId()+" "+stu.getName()+" "+stu.getSpeciality()+" "+stu.getQQ()).toString()+"\n"; Log.i("--------->", Result); list.add(Result); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); } } public class Student { long Id; String Name; String Speciality; long QQ; public Student(long id, String name, String speciality, long qQ) { super(); Id = id; Name = name; Speciality = speciality; QQ = qQ; } public Student() { super(); } public long getId() { return Id; } public String getName() { return Name; } public long getQQ() { return QQ; } public String getSpeciality() { return Speciality; } public void setId(long id) { Id = id; } public void setName(String name) { Name = name; } public void setQQ(long qQ) { QQ = qQ; } public void setSpeciality(String speciality) { Speciality = speciality; } } } 运行效果![](http://hi.csdn.net/attachment/201109/12/0_1315833052cNsq.gif)