分享到 新浪微博騰訊微博收藏+13 踩 頂 0 SAX是一種佔用記憶體少且解析速度快的解析器,它採用的是事件啟動,它不需要解析完整個文檔,而是按照內容順序 看文檔某個部分是否符合xml語法,如果符合就觸發相應的事件,所謂的事件就是些回檔方法(callback),這些方法 定義在ContentHandler中,下面是其主要方法:
startDocument:當遇到文檔的時候就觸發這個事件 調用這個方法 可以在其中做些預處理工作
startElement: (String namespaceURI,String localName,String qName,Attributes atts)當遇開始標籤的時候就會觸發這個方法。
endElement(String uri,String localName,String name):當遇到結束標籤時觸發這個事件,調用此法可以做些善後工作。
charachers(char [] ch,int start,int length):當遇到xml內容時觸發這個方法,用new String(ch,start,length)可以接受內容。
首先在 res目錄下新建一個raw資料夾,然後再裡面麼新建一個student.xml
student.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
03 |
< student id = "2009081315" > |
05 |
< speciality >计算机科学与技术</ speciality > |
09 |
< student id = "2009081316" > |
11 |
< speciality >网络工程</ speciality > |
14 |
< student id = "2009081318" > |
16 |
< speciality >软件工程</ speciality > |
[代码] Student.java
01 |
public class Student { |
08 |
public Student( long id, String name, String speciality, long qQ) { |
12 |
Speciality = speciality; |
25 |
public String getName() { |
33 |
public String getSpeciality() { |
37 |
public void setId( long id) { |
41 |
public void setName(String name) { |
45 |
public void setQQ( long qQ) { |
49 |
public void setSpeciality(String speciality) { |
50 |
Speciality = speciality; |
[代码] StudentHandler.java
03 |
import java.util.List; |
05 |
import org.xml.sax.Attributes; |
06 |
import org.xml.sax.SAXException; |
07 |
import org.xml.sax.helpers.DefaultHandler; |
09 |
import Android.util.Log; |
11 |
public class StudentHandler extends DefaultHandler { |
14 |
private String preTAG; |
15 |
private List<Student> ListStudent; |
18 |
public StudentHandler() { |
22 |
public StudentHandler(List<Student> listStudent) { |
24 |
ListStudent = listStudent; |
28 |
public void startDocument() throws SAXException { |
29 |
// TODO Auto-generated method stub |
30 |
Log.i( "------>" , "文档开始" ); |
31 |
super .startDocument(); |
34 |
public void startElement(String uri, String localName, String qName, |
35 |
Attributes attributes) throws SAXException { |
38 |
Log.i( "localName-------->" , localName); |
40 |
if ( "student" .equals(localName)) { |
42 |
stu.setId(Long.parseLong(attributes.getValue( 0 ))); |
44 |
for ( int i = 0 ; i < attributes.getLength(); i++) { |
45 |
//Log.i("attributes-------->", attributes.getValue(i)); |
46 |
Log.i( "attributes-------->" ,String.valueOf(stu.getId())); |
49 |
super .startElement(uri, localName, qName, attributes); |
52 |
public void endDocument() throws SAXException { |
54 |
Log.i( "------>" , "文档结束" ); |
58 |
public void endElement(String uri, String localName, String qName) |
61 |
if ( "student" .equals(localName)) { |
63 |
Log.i( "-------->" , "一个元素解析完成" ); |
65 |
super .endElement(uri, localName, qName); |
69 |
public void characters( char [] ch, int start, int length) |
73 |
if ( "name" .equals(preTAG)) { |
74 |
dateString= new String(ch,start,length); |
75 |
stu.setName(dateString); |
76 |
Log.i( "name=" , stu.getName()); |
77 |
} else if ( "speciality" .equals(preTAG)) { |
78 |
dateString= new String(ch,start,length); |
79 |
stu.setSpeciality(dateString); |
80 |
Log.i( "speciality=" , stu.getSpeciality()); |
81 |
} else if ( "qq" .equals(preTAG)) { |
82 |
dateString= new String(ch,start,length); |
83 |
stu.setQQ(Long.parseLong((dateString))); |
84 |
Log.i( "QQ=" , String.valueOf(stu.getQQ())); |
87 |
super .characters(ch, start, length); |
91 |
public List<Student> getListStudent() { |
95 |
public void setListStudent(List<Student> listStudent) { |
96 |
ListStudent = listStudent; |
[代码] XMl_Sax1Activity.java
01 |
import java.util.ArrayList; |
02 |
import java.util.Iterator; |
03 |
import java.util.List; |
05 |
import javax.xml.parsers.SAXParserFactory; |
07 |
import org.xml.sax.InputSource; |
08 |
import org.xml.sax.XMLReader; |
11 |
import Android.app.Activity; |
12 |
import Android.os.Bundle; |
13 |
import Android.util.Log; |
14 |
import Android.view.View; |
15 |
import Android.view.View.OnClickListener; |
16 |
import Android.widget.Adapter; |
17 |
import Android.widget.ArrayAdapter; |
18 |
import Android.widget.Button; |
19 |
import Android.widget.ListView; |
20 |
import Android.widget.TextView; |
22 |
public class XMl_Sax1Activity extends Activity { |
23 |
private Button button; |
24 |
private TextView textView; |
25 |
private ListView listView; |
26 |
private List<String> list= new ArrayList<String>(); |
27 |
public void onCreate(Bundle savedInstanceState) { |
28 |
super .onCreate(savedInstanceState); |
29 |
setContentView(R.layout.main); |
30 |
button=(Button)findViewById(R.id.button1); |
31 |
textView=(TextView)findViewById(R.id.textView1); |
32 |
listView=(ListView) findViewById(R.id.listView1); |
33 |
//InputSource xMLResourceString=new InputSource(XMl_Sax1Activity.this.getResources().openRawResource(R.raw.student)); |
34 |
button.setOnClickListener( new ButtonListener()); |
37 |
class ButtonListener implements OnClickListener{ |
40 |
public void onClick(View v) { |
42 |
List<Student> students=parserXMl(); |
43 |
for (Iterator iterator = students.iterator(); iterator.hasNext();) { |
44 |
Student student = (Student) iterator.next(); |
45 |
list.add(String.valueOf(student.getId())+ " " +student.getName()+ " " +student.getSpeciality()+ " " +String.valueOf((student.getQQ()))); |
48 |
ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(), Android.R.layout.simple_list_item_1, list); |
49 |
listView.setAdapter(adapter); |
54 |
private List<Student> parserXMl() |
56 |
SAXParserFactory factory=SAXParserFactory.newInstance(); |
57 |
List<Student>students= null ; |
60 |
XMLReader reader=factory.newSAXParser().getXMLReader(); |
61 |
students= new ArrayList<Student>(); |
62 |
reader.setContentHandler( new StudentHandler(students)); |
63 |
reader.parse( new InputSource(XMl_Sax1Activity. this .getResources().openRawResource(R.raw.student))); |
64 |
for (Iterator iterator = students.iterator(); iterator.hasNext();) { |
65 |
student = (Student) students.iterator(); |
67 |
students.add(student); |
68 |
} catch (Exception e) { |
69 |
// TODO: handle exception |
[代码] 布局文件main.xml
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:Android = "http://schemas.android.com/apk/res/android" |
03 |
Android:orientation = "vertical" |
04 |
Android:layout_width = "fill_parent" |
05 |
Android:layout_height = "fill_parent" |
08 |
< Button Android:text = "SAX解析" android:id = "@+id/button1" android:layout_height = "wrap_content" android:layout_width = "match_parent" ></ Button > |
09 |
< TextView Android:text = "" android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ TextView > |
10 |
< ListView Android:layout_height = "wrap_content" android:id = "@+id/listView1" android:layout_width = "match_parent" ></ ListView > |
留言列表