SAX(simple API for XML)是一种XML解析的替代方法。相比于DOM,SAX是一种速度更快,更有效的方法。
在res资源目录下创建一个叫做raw的文件夹,在文件夹中创建如下的xml文件
小王 19 男 王子 29 男 小张 27 女
创建该xml对应的实体类,
package com.example.entity;public class Persons{ private Integer id; private String name; private String age; private String sex; public Persons() { } public Persons(Integer id, String name, String age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
package com.example.entity;public class Persons{ private Integer id; private String name; private String age; private String sex; public Persons() { } public Persons(Integer id, String name, String age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
MVC思想中创建c这一层,也就是控制层,
package com.example.oper;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import com.example.entity.Persons;public class SaxPar{ private Listlist; private String flag; public List parer(InputStream is) throws Exception { //生成该类的对象 MyDefaultHandler myDefaultHandler = new MyDefaultHandler(); //这个方法跟dom有点类似,都是要创建一个工厂,然后实例化 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); //将is流读进来进行操作 saxParser.parse(is, myDefaultHandler); return myDefaultHandler.getList(); } private class MyDefaultHandler extends DefaultHandler { private List list; private Persons p; public List getList() { return list; } @Override public void characters(char[] ch, int start, int length) throws SAXException { if(flag != null) { if(flag.equals("name")) { p.setName(new String(ch,start,length)); } if(flag.equals("sex")) { p.setSex(new String(ch,start,length)); } if(flag.equals("age")) { p.setAge(new String(ch,start,length)); } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(localName.equals("person")) { list.add(p); } flag = null; } @Override public void startDocument() throws SAXException { list = new ArrayList (); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(localName.equals("person")) { p = new Persons(); p.setId(Integer.parseInt(attributes.getValue(0))); } flag = localName; } }}
在activity中应用
package com.example.sax;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import com.example.entity.Persons;import com.example.oper.SaxPar;public class MainActivity extends Activity{ private Listlist; private ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView)findViewById(R.id.listView1); List mData = new ArrayList (); InputStream is =getResources().openRawResource(R.raw.persons); try { List list = new SaxPar().parer(is); for (Persons person : list) { mData.add(person.getId()+"-"+person.getName()+"-"+person.getSex()+"-"+person.getAge()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } mListView.setAdapter(new ArrayAdapter (MainActivity.this, android.R.layout.simple_list_item_1,mData)); }}