关于使用SAX解析XML文件也没什么要说明的,直接上代码吧。
关键如下:
public class PersonHandler extends DefaultHandler { private String preTAG; private List<PersonInfo> personList; private PersonInfo per; public PersonHandler() { super(); } public PersonHandler(List<PersonInfo> personList) { super(); this.personList = personList; } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { preTAG = localName; if ("person".equals(localName)) { per = new PersonInfo(); 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); } super.endElement(uri, localName, qName); } public void characters(char[] ch, int start, int length) throws SAXException { if ("name".equals(preTAG)) { per.setName(new String(ch, start, length)); } else if ("age".equals(preTAG)) { per.setAge("" + Integer.parseInt(new String(ch, start, length))); } super.characters(ch, start, length); } public List<PersonInfo> getPersonList() { return personList; } public void setPersonList(List<PersonInfo> personList) { this.personList = personList; } }这里需要一个辅助类:
public class PersonInfo { private Integer id; private String name; private String age; public PersonInfo() { } public PersonInfo(Integer id, String name, String age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }主界面Activity入口如下:
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.sax_button); listView = (ListView) findViewById(R.id.lv); btn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { List<PersonInfo> persons = parserXMl(); for (Iterator iterator = persons.iterator(); iterator.hasNext();) { PersonInfo person = (PersonInfo) 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<PersonInfo> parserXMl() { List<PersonInfo> persons = new ArrayList<PersonInfo>(); PersonInfo 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 = (PersonInfo) persons.iterator(); } persons.add(person); } catch (Exception e) { } return persons; } }解析结果如下: