SAX(Simple API for XML),是指一种接口,或者一个软件包。
DOM:文档驱动。DOM在解析文件之前把整个文档装入内存,处理大型文件时其性能很差,是由于DOM的树结构所造成的,此结构占用的内存较多。
SAX:事件驱动型的XML解析方式。顺序读取XML文件,不需要一次全部装载整个文件。当遇到像文件开头,文档结束,或者标签开头与标签结束时,会触发一个事件,用户通过在其回调事件中写入处理代码来处理XML文件,适合对XML的顺序访问,且是只读的。
由于移动设备的内存资源有限,SAX的顺序读取方式更适合移动开发。
SAX解析XML步骤
①创建XML解析处理器。
②创建SAX解析器。
③将XML解析处理器分配给解析器。
④对文档进行解析,将每个事件发送给处理器。
下面是SAX解析XML文件的代码程序,完整代码下载地址:android_sax.rar
MainActivity:
- /**
- * MainActivity
- *
- * @author zuolongsnail
- *
- */
- public class MainActivity extends Activity {
- private Button parseBtn;
- private ListView listView;
- private ArrayAdapter<Person> adapter;
- private ArrayList<Person> persons;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- parseBtn = (Button) findViewById(R.id.parse);
- listView = (ListView) findViewById(R.id.list);
- parseBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- persons = MainActivity.this.readXml();
- adapter = new ArrayAdapter<Person>(MainActivity.this,
- android.R.layout.simple_expandable_list_item_1, persons);
- listView.setAdapter(adapter);
- }
- });
- }
- private ArrayList<Person> readXml() {
- InputStream file = this.getClass().getClassLoader()
- .getResourceAsStream("persons.xml");
- // ①创建XML解析处理器
- SAXXmlContentHandler contentHandler = new SAXXmlContentHandler();
- try {
- // 创建一个SAXParserFactory
- SAXParserFactory factory = SAXParserFactory.newInstance();
- // ②创建SAX解析器
- SAXParser parser = factory.newSAXParser();
- // ③将XML解析处理器分配给解析器
- // ④对文档进行解析,将每个事件发送给处理器。
- parser.parse(file, contentHandler);
- file.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return contentHandler.getBooks();
- }
- }
XML解析处理器:
- /**
- * 解析XML处理器
- * @author zuolongsnail
- *
- */
- public class SAXXmlContentHandler extends DefaultHandler {
- private ArrayList<Person> persons;
- private Person person;
- private String tagName;
- public ArrayList<Person> getBooks() {
- return persons;
- }
- @Override
- public void startDocument() throws SAXException {
- this.persons = new ArrayList<Person>();
- Log.e("SAXXmlContentHandler", "开始解析XML文件");
- }
- @Override
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- Log.e("SAXXmlContentHandler", "读取标签");
- this.tagName = localName;
- if(this.tagName.equals("person")){
- person = new Person();
- person.setId(Integer.parseInt(attributes.getValue(0)));
- }
- }
- @Override
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- Log.e("SAXXmlContentHandler", "根据tagName获取标签的内容");
- if (this.tagName != null) {
- String data = new String(ch, start, length);
- if (this.tagName.equals("name")) {
- this.person.setName(data);
- } else if (this.tagName.equals("age")) {
- this.person.setAge(Integer.parseInt(data));
- }
- }
- }
- @Override
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if(localName.equals("person")){
- this.persons.add(person);
- }
- this.tagName = null;
- }
- }
persons.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <persons>
- <person id="1">
- <name>张三</name>
- <age>24</age>
- </person>
- <person id="2">
- <name>李四</name>
- <age>25</age>
- </person>
- <person id="3">
- <name>王五</name>
- <age>26</age>
- </person>
- </persons>
Person实体类:
- /**
- * Person实体类
- * @author zuolongsnail
- *
- */
- public class Person {
- private int id;
- private String name;
- private int age;
- public Person() {
- }
- public Person(int id, String name, int age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "姓名:" + this.name + ",年龄:" + this.age;
- }
- }
main.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="@string/hello" />
- <Button android:id="@+id/parse" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="SAX解析XML" />
- <ListView android:id="@+id/list" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </ListView>
- </LinearLayout>
程序运行截图:
点击解析按钮后为