Android SAX 解析 XML

openkk 12年前

下面是效果图,代码中有详细解释,

Android SAX 解析 XML

 res/raw/person.xml

<?xml version="1.0" encoding="UTF-8"?>  <persons>      <person id="001">          <name>张三</name>          <age>22</age>      </person>            <person id="002">          <name>李四</name>          <age>23</age>      </person>            <person id="003">          <name>王五</name>          <age>24</age>      </person>  </persons>

PersonHandler.java

public class PersonHandler extends DefaultHandler {    private String preTAG;          private List<Person> personList;        private Person per;               public PersonHandler() {            super();        }        public PersonHandler(List<Person> personList) {            super();            this.personList = personList;        }      public void startDocument() throws SAXException {    Log.i("Mimi---", "文档开始");     super.startDocument();   }   public void startElement(String uri, String localName, String qName,     Attributes attributes) throws SAXException {    preTAG=localName;     if ("person".equals(localName)) {      per=new Person();     per.setId(new Integer(attributes.getValue(0)));    }     super.startElement(uri, localName, qName, attributes);   }     public void endElement(String uri, String localName, String qName)     throws SAXException {    preTAG = "";    if ("person".equals(localName)){     personList.add(per);     Log.i("Mimi---", "一个元素解析完成");    }    super.endElement(uri, localName, qName);   }     public void endDocument() throws SAXException {    Log.i("Mimi---", "文档结束");    super.endDocument();   }     public void characters(char[] ch, int start, int length)     throws SAXException {    if ("name".equals(preTAG)) {     per.setName(new String(ch, start, length));     Log.i("name=", per.getName());    } else if ("age".equals(preTAG)) {     per.setAge(Integer.parseInt(new String(ch, start, length)));     Log.i("age=", per.getAge()+"");    }    super.characters(ch, start, length);   }   public List<Person> getPersonList() {    return personList;   }   public void setPersonList(List<Person> personList) {    this.personList = personList;   }     }

SAXActivity.java

public class SAXActivity extends Activity {   Button btn;   ListView listView;   List<String> list=new ArrayList<String>();      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          btn=(Button) findViewById(R.id.button);          listView=(ListView) findViewById(R.id.listView);          btn.setOnClickListener(new OnClickListener(){     public void onClick(View arg0) {      List<Person> persons=parserXMl();      for (Iterator iterator = persons.iterator(); iterator .hasNext();) {       Person person = (Person) iterator.next();       list.add(String.valueOf("ID:"+person.getId()) + " 姓名: "         + person.getName() + " 年龄: " + person.getAge());      }      ArrayAdapter<String> adapter = new ArrayAdapter<String>(        getApplicationContext(),        android.R.layout.simple_list_item_1,        list);      listView.setAdapter(adapter);     }          });      }      private List<Person> parserXMl()    {    List<Person> persons = new ArrayList<Person>();    Person person = null;    try {     //创建解析器     SAXParserFactory sax = SAXParserFactory.newInstance();     XMLReader reader = sax.newSAXParser().getXMLReader();     //为reader设置内容处理器     reader.setContentHandler(new PersonHandler(persons));     //开始解析文件     reader.parse(new InputSource(getResources().openRawResource(R.raw.person)));     //循环保存解析内容到persons     for (Iterator iterator = persons.iterator(); iterator.hasNext();) {      person = (Person) persons.iterator();     }     persons.add(person);    } catch (Exception e) {    }    return persons;   }  }