使用SAX解析XML文件
生活随笔
收集整理的這篇文章主要介紹了
使用SAX解析XML文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
關于使用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 {// 創(chuàng)建解析器SAXParserFactory sax = SAXParserFactory.newInstance();XMLReader reader = sax.newSAXParser().getXMLReader();// 為reader設置內(nèi)容處理器reader.setContentHandler(new PersonHandler(persons));// 開始解析文件reader.parse(new InputSource(getResources().openRawResource(R.raw.person)));// 循環(huán)保存解析內(nèi)容到personsfor (Iterator iterator = persons.iterator(); iterator.hasNext();) {person = (PersonInfo) persons.iterator();}persons.add(person);} catch (Exception e) {}return persons;} }解析結果如下:
總結
以上是生活随笔為你收集整理的使用SAX解析XML文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Toast的另类应用及另类“拦截”Hom
- 下一篇: Android高仿IOS和QQ的弹出对话