解析XML文件一般使用XmlPullParserFactory方式进行解析:
(1)需要解析的XML建立一个raw文件夹放入其中
解析的代码如person.xml:
<?xml version = "1.0" encoding = "UTF-8"?> <persons> <person id = "23"> <name>张老师</name> <age>21</age> </person> <person id = "20"> <name>李老师</name> <age>25</age> </person> </persons>(2)需要建立一个和属性相同的实体类person.java
public class Person { private String id; private String name; private int age; public Person() { // TODO Auto-generated constructor stub } public Person(String id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }(3)写一个XmlPullParserFactory的解析工具ParseTool.java:
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.content.Context; import android.content.res.Resources; import com.qianfeng.android1524_spinner3.R; import com.qianfeng.android1524_spinner3.entity.Person; public class ParseTool { private Context context; public ParseTool(Context context) { this.context = context; } public List<Person> parse() throws XmlPullParserException, IOException { List<Person> list = null; Person person = null; XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); //获取被解析的xml文件的读取流 Resources res = context.getResources(); InputStream inputStream = res.openRawResource(R.raw.person); parser.setInput(inputStream,"utf-8"); int event = parser.getEventType(); while(event!=XmlPullParser.END_DOCUMENT) { switch(event) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: String tagName = parser.getName(); if("person".equals(tagName)) { person = new Person(); int count = parser.getAttributeCount(); for(int i=0;i<count;i++) { String attrName = parser.getAttributeName(i); String attrValue = parser.getAttributeValue(i); if("id".equals(attrName)) person.setId(attrValue); } } else if("name".equals(tagName)) person.setName(parser.nextText()); else if("age".equals(tagName)) person.setAge(Integer.parseInt(parser.nextText())); break; case XmlPullParser.END_TAG: if("person".equals(parser.getName())) { list.add(person); } break; } event = parser.next(); } return list; } }(4)在Activity_main.xml中添加spinner组件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>(5)最后在MainActivity中得到解析data并且使用ArrayAdapter进行适配:
package com.qianfeng.android1524_spinner3; import java.io.IOException; import java.util.List; import org.xmlpull.v1.XmlPullParserException; import com.qianfeng.android1524_spinner3.entity.Person; import com.qianfeng.android1524_spinner3.tool.ParseTool; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import android.app.Activity; public class MainActivity extends Activity { private Spinner spinner; private List<Person> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner); ParseTool tool = new ParseTool(this); try { list = tool.parse(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //适配器是调用集合中每个对象的toString()方法,把toString()方法的返回值作为数据项来显示 ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, list); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Person ren = list.get(position); Toast.makeText(MainActivity.this, ren.getName(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } }最后启动模拟器运行后就可以看到效果了!