用的person.xml,Person.java文件同上一篇介绍Pull解析一样。
1 person.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="000111"> <name>tom</name> <age>230</age> </person> <person id="000222"> <name>jerry</name> <age>233</age> </person> </persons>
2 Person.java
package com.example.xml_saxparser; public class Person { private Integer id; private String name; private Short age; public Person(){ } public Person(Integer id, String name, Short age) { super(); this.id = id; this.name = name; this.age = age; } public Person( String name, Short age) { super(); 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 Short getAge() { return age; } public void setAge(Short age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
3 SAX解析的handler,要继承DefaultHandler,选择要重写的方法,以下是主要的5个方法。
package com.example.xml_saxparser; import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SAXforHandler extends DefaultHandler { private List<Person> persons; private Person person; private String perTag; public List<Person> getPersons(){ return persons; } @Override public void startDocument() throws SAXException { persons = new ArrayList<Person>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("person".equals(localName)){ for(int i = 0; i < attributes.getLength(); i++){ person = new Person(); person.setId(Integer.valueOf(attributes.getValue(i))); } } perTag = localName; } @Override public void characters(char[] ch, int start, int length) throws SAXException { String data = new String(ch, start, length).trim(); if(!"".equals(data.trim())){ } if("name".equals(perTag)){ person.setName(data); }else if("age".equals(perTag)){ person.setAge(new Short(data)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if("person".equals(localName)){ persons.add(person); person = null; } perTag = null; } @Override public void endDocument() throws SAXException { } }
4 单元测试,在日志中打印结果。
package com.example.xml_saxparser; import java.io.InputStream; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; import android.test.AndroidTestCase; import android.util.Log; import com.example.xml_saxparser.MainActivity; import com.example.xml_saxparser.Person; import com.example.xml_saxparser.SAXforHandler; public class SAXJUnitTest extends AndroidTestCase { public void testSax() throws Exception{ InputStream in = MainActivity.class.getClassLoader().getResourceAsStream("person.xml"); SAXforHandler handler = new SAXforHandler(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = spf.newSAXParser(); saxParser.parse(in, handler); List<Person> list = handler.getPersons(); in.close(); for(Person p : list){ Log.i("Person", p.toString()); } } }
时间: 2024-11-08 14:24:07