Android 开发工具类 21_SAXForHandler
解析 XML 有两种形式:
1、XMLReader
1 XMLReaser xmlReader = saxParser.getXMLReader(); 2 xmlReadeer.setContentHandler(hangdler); 3 xmlREader.pase(new InputSource(is));
2、SAXParser
saxParser.parse(is, handler);
其实 SAXParser 是 JAXP(Java API for XML Processing) 对 XMLReader 的一个封装。
1 import java.util.ArrayList; 2 import java.util.List; 3 4 import org.xml.sax.Attributes; 5 import org.xml.sax.SAXException; 6 import org.xml.sax.helpers.DefaultHandler; 7 8 import android.util.Log; 9 10 import com.wangjialin.domain.Person; 11 12 public class SAXForHandler extends DefaultHandler { 13 14 private static final String TAG = "SAXForHandler"; 15 private List<Person> persons; 16 private String perTag ; // 通过此变量,记录前一个标签的名称。 17 Person person; // 记录当前 Person 18 19 public List<Person> getPersons() { 20 return persons; 21 } 22 23 // 适合在此事件中触发初始化行为。 24 public void startDocument() throws SAXException { 25 persons = new ArrayList<Person>(); 26 Log.i(TAG , "***startDocument()***"); 27 } 28 29 public void startElement(String uri, String localName, String qName, 30 Attributes attributes) throws SAXException { 31 32 if("person".equals(localName)){ 33 for ( int i = 0; i < attributes.getLength(); i++ ) { 34 Log.i(TAG ,"attributeName:" + attributes.getLocalName(i) 35 + "_attribute_Value:" + attributes.getValue(i)); 36 person = new Person(); 37 person.setId(Integer.valueOf(attributes.getValue(i))); 38 } 39 } 40 perTag = localName; 41 Log.i(TAG , qName+"***startElement()***"); 42 } 43 44 public void characters(char[] ch, int start, int length) throws SAXException { 45 46 String data = new String(ch, start, length).trim(); 47 if(!"".equals(data.trim())){ 48 Log.i(TAG ,"content: " + data.trim()); 49 } 50 if("name".equals(perTag)){ 51 person.setName(data); 52 }else if("age".equals(perTag)){ 53 person.setAge(new Short(data)); 54 } 55 } 56 57 public void endElement(String uri, String localName, String qName) throws SAXException { 58 59 Log.i(TAG , qName + "***endElement()***"); 60 if("person".equals(localName)){ 61 persons.add(person); 62 person = null; 63 } 64 perTag = null; 65 } 66 67 public void endDocument() throws SAXException { 68 69 Log.i(TAG , "***endDocument()***"); 70 } 71 }
Person.java
1 package com.wangjialin.domain; 2 3 public class Person { 4 5 private Integer id; 6 private String name; 7 private Short age; 8 9 public Person(){} 10 11 public Person(Integer id, String name, Short age) { 12 this.id = id; 13 this.name = name; 14 this.age = age; 15 } 16 17 public Person(String name, Short age) { 18 this.name = name; 19 this.age = age; 20 } 21 22 public Integer getId() { 23 return id; 24 } 25 26 public void setId(Integer id) { 27 this.id = id; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 38 public Short getAge() { 39 return age; 40 } 41 42 public void setAge(Short age) { 43 this.age = age; 44 } 45 46 @Override 47 public String toString() { 48 return "id=" + id + ",name=" + name+ ",age=" + age; 49 } 50 51 }
JUnit 测试
1 public void testSAXGetPersons() throws Throwable{ 2 3 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("wangjialin.xml"); 4 SAXForHandler saxForHandler = new SAXForHandler(); 5 // 获取 SAX 解析器的工厂对象 6 SAXParserFactory spf = SAXParserFactory.newInstance(); 7 // 创建解析器对象 8 SAXParser saxParser = spf.newSAXParser(); 9 // 设定解析的文件和自己定义的事件处理器对象 10 saxParser.parse(inputStream, saxForHandler); 11 12 List<Person> persons = saxForHandler.getPersons(); 13 inputStream.close(); 14 15 for(Person person:persons){ 16 Log.i(TAG, person.toString()); 17 } 18 }
【参考】
http://blog.csdn.net/redarmy_chen/article/details/12951649