在Android平台上可以使用Simple API for XML(SAX) 、 Document Object Model(DOM)和Android附带的pull解析器解析XML文件。
下面是本例子要解析的XML文件:
文件名称:itcast.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>李明</name> <age>30</age> </person> <person id="20"> <name>李向梅</name> <age>25</age> </person> </persons>
public class Person { private Integer id; private String name; private Short 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; } }
使用SAX读取XML文件
SAX是一个解析速度快并且占用内存少的xml解析器,非常适合用于Android等移动设备。 SAX解析XML文件采用的是事件驱动,也就是说,它并不需要解析完整个文档,在按内容顺序解析文档的过程中,SAX会判断当前读到的字符是否合法XML语法中的某部分,如果符合就会触发事件。所谓事件,其实就是一些回调(callback)方法,这些方法(事件)定义在ContentHandler接口。
下面是一些ContentHandler接口常用的方法:
读到的标签及内容 触发事件 {文档开始} startDocument() <persons> startElement(, "persons", null, "{Attributes}") "\n\t" characters("<persons>...</persons>", "12", "2") <person> startElement(, "person", null, "{Attributes}") "\n\t\t" characters("<persons>...</persons>", "31", "3") <name> startElement(, "name", null, "{Attributes}") "李明" characters("<persons>...</persons>", "40", "2") </name> endElement("", "name", null) "\n\t\t" characters("<persons>...</persons>", "50", "3") <age> startElement(, "age", null, "{Attributes}") "30" characters("<persons>...</persons>", "58", "2") </age> endElement("", "age", null) "\n\t" characters("<persons>...</persons>", "67", "2") </person> endElement("", "person", null) "\n\t" characters("<persons>...</persons>", "79", "2") <person> startElement(, "person", null, "{Attributes}") "\n\t\t" characters("<persons>...</persons>", "98", "3") <name> startElement(, "name", null, "{Attributes}") "李向梅" characters("<persons>...</persons>", "107", "3") </name> endElement("", "name", null) "\n\t\t" characters("<persons>...</persons>", "118", "3") <age> startElement(, "age", null, "{Attributes}") "25" characters("<persons>...</persons>", "126", "2") </age> endElement("", "age", null) "\n\t" characters("<persons>...</persons>", "135", "2") </person> endElement("", "person", null) "\n" characters("<persons>...</persons>", "147", "1") </persons> endElement("", "persons", null) {文档结束} endDocument()
public static List<Person> readXML(InputStream inStream) { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxParser = spf.newSAXParser(); //创建解析器 //设置解析器的相关特性,http://xml.org/sax/features/namespaces = true 表示开启命名空间特性 saxParser.setProperty("http://xml.org/sax/features/namespaces",true); XMLContentHandler handler = new XMLContentHandler(); saxParser.parse(inStream, handler); inStream.close(); return handler.getPersons(); } catch (Exception e) { e.printStackTrace(); } return null; } import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import cn.itcast.xml.domain.Person; public class XMLContentHandler extends DefaultHandler { private List<Person> persons = null; private Person currentPerson; private String tagName = null;//当前解析的元素标签 public List<Person> getPersons() { return persons; } /* * 接收文档的开始的通知。 */ @Override public void startDocument() throws SAXException { persons = new ArrayList<Person>(); } /* * 接收字符数据的通知。 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if(tagName!=null){ String data = new String(ch, start, length); if(tagName.equals("name")){ this.currentPerson.setName(data); }else if(tagName.equals("age")){ this.currentPerson.setAge(Short.parseShort(data)); } } } /* * 接收元素开始的通知。 * 参数意义如下: * namespaceURI:元素的命名空间 * localName :元素的本地名称(不带前缀) * qName :元素的限定名(带前缀) * atts :元素的属性集合 */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { if(localName.equals("person")){ currentPerson = new Person(); currentPerson.setId(Integer.parseInt(atts.getValue("id"))); } this.tagName = localName; } /* * 接收文档的结尾的通知。 * 参数意义如下: * uri :元素的命名空间 * localName :元素的本地名称(不带前缀) * name :元素的限定名(带前缀) * */ @Override public void endElement(String uri, String localName, String name) throws SAXException { if(localName.equals("person")){ persons.add(currentPerson); currentPerson = null; } this.tagName = null; } }