由于XmlPullParser有不同的版本,PC端的与Android端好多地方处理问题的方式都不同。
PC端的下载地址:XmlPullParser下载
Android本身就集成得有这种处理问题的类,闲话少扯,来点真实的:
1、Xml文件,参照之一
2、定义一个ListViewItem的layout文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="30dp"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tvName"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tvSex"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tvAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
3、定义一个ListViewAdapter类
public class ListViewAdapter extends BaseAdapter {
private ArrayList<Person> persons;
private LayoutInflater mInflater;
public ListViewAdapter(Context context,ArrayList<Person> persons) {
super();
this.persons = persons;
mInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return persons.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
convertView=mInflater.inflate(R.layout.listitem, null);
TextView tvId=(TextView)convertView.findViewById(R.id.tvId);
TextView tvName=(TextView)convertView.findViewById(R.id.tvName);
TextView tvSex=(TextView)convertView.findViewById(R.id.tvSex);
TextView tvAge=(TextView)convertView.findViewById(R.id.tvAge);
Person person=persons.get(position);
Log.i("Test", "Id="+person.getId()+",Name="+person.getName()+",Sex="+person.getSex()+",Age="+person.getAge());
person.setId(person.getId());
person.setName(person.getName());
person.setSex(person.getSex());
person.setAge(person.getAge());
return convertView;
}
}
4、定义一个PersonPullService类
public class PersonPullService {
public static ArrayList<Person> getPersons(InputStream inStream) throws Exception
{
XmlPullParser pullParser=Xml.newPullParser();
pullParser.setInput(inStream, "UTF-8");
int event=pullParser.getEventType();
ArrayList<Person> persons=null;;
Person person=null;
while(event!=XmlPullParser.END_DOCUMENT)
{
switch(event)
{
case XmlPullParser.START_DOCUMENT:
persons=new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if("person".equals(pullParser.getName()))
{
int id=new Integer(pullParser.getAttributeValue(0));
person=new Person();
person.setId(id);
if(person!=null)
{
if("name".equals(pullParser.getName()))
person.setName(pullParser.nextText());
else if("sex".equals(pullParser.getName()))
person.setSex(pullParser.nextText());
else if("age".equals(pullParser.getName()))
person.setAge(new Short(pullParser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if("person".equals(pullParser.getName()))
{
persons.add(person);
person=null;
}
break;
}
event=pullParser.next();
}
return persons;
}
}
5、Activity类的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.testpull.MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
6、Activity类的代码
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)this.findViewById(R.id.lv);
String path=Environment.getExternalStorageDirectory()+"/test13/persons.xml";
try
{
Log.i("Test", path);
InputStream inputStream=new FileInputStream(new File(path));
if(inputStream==null)
Log.i("Test", "没数据");
ArrayList<Person> persons=PersonPullService.getPersons(inputStream);
Log.i("Test", String.valueOf(persons.size()));
ListViewAdapter adapter=new ListViewAdapter(this,persons);
lv.setAdapter(adapter);
}
catch(Exception e)
{
Log.i("Test", e.toString());
e.printStackTrace();
}
}
}
7、实体类参考之一。
Over了,时间有点紧,代码有点乱,大致意思就是这样!!