SAX解析XML
SAX是Simple API for XML的缩写,SAX是基于事件驱动的,在用SAX解析xml文档时候,在读取到文档开始和结束标签时候就会回调一个事件,在读取到其它节点与内容时也会回调一个事件.事件源是org.xml.sax包中的XMLReader,它通过parser()方法来解析XML文档,并产生事件.事件处理器是DefaultHandler,与DOM比较而言,SAX是一种轻量型的方法.当需要一个速度快的解析器并且希望最大限度的减少应用程序的内存占用时,通常可以使用SAX解析.
具体思路是:
*创建SAXParserFactory对象
SAXParserFactoryspf = SAXParserFactory.newInstance();
*根据SAXParserFactory.newSAXParser()方法返回一个SAXParser解析器
SAXParsersp = spf.newSAXParser();
*根据SAXParser解析器获取事件源对象XMLReader
XMLReader reader = sp.getXMLReader();
*实例化一个DefaultHandler对象(StudentHandler是一事件处理器)
StudentHandler handler = newStudentHandler(students);
*连接事件源对象XMLReader到事件处理DefaultHander
reader.setContentHandler(handler);
*调用XMLReader的parse方法从输入源获取到xml数据
*通过DefaultHandler返回我们需要的数据集合
注:重点在于DefaultHandler对象中对每一个元素节点,属性,文本内容,文档内容的处理.
***************相关代码*******
1 //实例化一个DefaultHandler对象(StudentHandler是一事件处理器) 2 3 4 public class StudentHandler extends DefaultHandler { 5 private List<Student> mstudent; 6 private String mtagname; 7 private Student student; 8 9 10 11 public StudentHandler() { 12 super(); 13 // TODO Auto-generated constructor stub 14 } 15 public StudentHandler(List<Student> mstudent) { 16 super(); 17 this.mstudent = mstudent; 18 } 19 //开始解析文档 20 @Override 21 public void startDocument() throws SAXException { 22 23 super.startDocument(); 24 } 25 //解析xml文档结尾 26 @Override 27 public void endDocument() throws SAXException { 28 29 super.endDocument(); 30 } 31 32 //解析开始标签 33 //元素节点 34 @Override 35 public void startElement(String uri, String localName, String qName, 36 Attributes attributes) throws SAXException { 37 this.mtagname = localName; 38 if("student".equals(mtagname)){ 39 student = new Student(); 40 student .setId(Integer.valueOf(attributes.getValue("id"))); 41 42 } 43 else if("age".equals(mtagname)){ 44 student .setAge(Integer.valueOf(attributes.getValue(0))); 45 } 46 super.startElement(uri, localName, qName, attributes); 47 } 48 //解析标签 结束 49 @Override 50 public void endElement(String uri, String localName, String qName) 51 throws SAXException { 52 this.mtagname= localName; 53 if("student".equals(mtagname)){ 54 mstudent.add(student); 55 student = null; 56 } 57 mtagname = ""; 58 Log.i("TAG", "infor"); 59 super.endElement(uri, localName, qName); 60 61 } 62 //解析文本标签 63 @Override 64 public void characters(char[] ch, int start, int length) 65 throws SAXException { 66 String name = new String (ch,start,length); 67 if(!name.trim().equals("")&&"name".equals(mtagname)){ 68 student.setName(name); 69 } 70 Log.i("TAG", "infor"); 71 super.characters(ch, start, length); 72 } 73 } 74 *****************************sax解析************* 75 //student是一个实体类,有name,age,参数和构造方法,toString输出 76 77 public class SaxParserActivity extends Activity { 78 private TextView mtextview; 79 private ListView mlistview; 80 @Override 81 protected void onCreate(Bundle savedInstanceState) { 82 83 super.onCreate(savedInstanceState); 84 setContentView(R.layout.domparser); 85 mtextview = (TextView) findViewById(R.id.textView1); 86 mlistview = (ListView) findViewById(R.id.listView1); 87 mtextview.setText("sax解析xml"); 88 89 List<Student> list = parser(); 90 ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,android.R.layout.simple_expandable_list_item_1,list); 91 mlistview.setAdapter(adapter); 92 93 } 94 private List<Student> parser() { 95 List <Student> list = new ArrayList<Student>(); 96 try { 97 // 获取工厂 98 SAXParserFactory sax = SAXParserFactory.newInstance(); 99 //获取sax解析器 100 SAXParser sf = sax.newSAXParser(); 101 //获取事件源 102 XMLReader xmlreader = sf.getXMLReader(); 103 //创建事件处理器 104 StudentHandler studenthandle = new StudentHandler(list); 105 // 将事件处理器设置到事件源 106 xmlreader.setContentHandler(studenthandle); 107 //触发解析事件,读取raw下面的文档 108 xmlreader.parse(new InputSource(getResources().openRawResource(R.raw.student))); 109 } catch (Exception e) { 110 // TODO Auto-generated catch block 111 Log.i("TAG",""+e.getMessage()); 112 } 113 return list; 114 } 115 }