首页 > 代码库 > XmlPullParser

XmlPullParser

可能遇到的错误 :路径错误

  java.lang.IllegalArgumentException: is == null

没有设置setInput()

  org.xmlpull.v1.XmlPullParserException: setInput() must be called first. (position:START_DOCUMENT null@1:1)

之所以程序写成下面这样,是因为4.0以后,不允许在主线程进行其他处理。否则会出现输入流异常

MainActivity.java

public class MainActivity extends Activity {

  MyHandler myHandler;
  private ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView);
    myHandler = new MyHandler();
    MyThread mythread = new MyThread();
    new Thread(mythread).start();
  }

  class MyHandler extends Handler{
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    Bundle b = msg.getData();
    List list = b.getParcelableArrayList("list");
    List<Map<String,String>> listItem = (List<Map<String, String>>) list.get(0);
    SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, listItem, R.layout.item, new String[]{"name","age"}, new int[]{R.id.name,R.id.age});
    listView.setAdapter(adapter);
    }
  }

  class MyThread implements Runnable{
    List<Person> list;
    List<Map<String,String>> listItem = new ArrayList<Map<String,String>>();
    Map<String,String> item =null;
    ArrayList array = new ArrayList();
    @Override
    public void run() {
      PersonService service = new PersonService();
      try {
        list = service.getFile();
        if(list!=null){
        for(Person p :list){
        item = new HashMap<String,String>();
        item.put("id", p.getId());
        item.put("name", p.getName());
        item.put("age", p.getAge());
        listItem.add(item);
      }
    }else{
      Toast.makeText(MainActivity.this, "没有解析出", Toast.LENGTH_SHORT).show();
      return;
    }
    array.add(listItem);
    Message msg = new Message();
    Bundle b = new Bundle();
    b.putParcelableArrayList("list", array);
    msg.setData(b);
    MainActivity.this.myHandler.sendMessage(msg);
    } catch (IOException e) {
      Toast.makeText(MainActivity.this, "文件错误", Toast.LENGTH_SHORT).show();
    } catch (XmlPullParserException e) {
      Toast.makeText(MainActivity.this, "解析错误", Toast.LENGTH_SHORT).show();
    }
  }

 }
}

 PersonService.java

public class PersonService {

  public List<Person> getFile() throws IOException, XmlPullParserException{
  InputStream input = this.getClass().getClassLoader().getResourceAsStream("person.xml");
  return parseXML(input);
}

public static List<Person> parseXML(InputStream input) throws XmlPullParserException, IOException{
  List<Person> list = null;
  Person person = null;
  XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
  int eventType = parser.getEventType();
  parser.setInput(input, "UTF-8");
  while(eventType != XmlPullParser.END_DOCUMENT){
  switch (eventType) {
    case XmlPullParser.START_DOCUMENT:
      list = new ArrayList<Person>();
    break;
    case XmlPullParser.START_TAG:
      if(parser.getName().equals("person")){
        person = new Person();
        String id = parser.getAttributeValue(0);
        person.setId(id);
      }
      if(person!=null){
      if(parser.getName().equals("name")){
        String name = parser.nextText();
        person.setName(name);
      }
      if(parser.getName().equals("age")){
        String age = parser.nextText();
        person.setAge(age);
      }
    }
    break;
    case XmlPullParser.END_TAG:
      if(parser.getName().equals("person")){
        list.add(person);
        person = null;
      }
    break;
    }
    eventType = parser.next();
  }
  return list;
}
}

 

 person.xml

<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person id="12">
<name>java</name>
<age>85</age>
</person>
<person id="15">
<name>Action</name>
<age>39</age>
</person>
</persons>