xmldefaulthandlerparserandroidArrayAdapterSAXParserFactory android
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對象中對每個元素節點,屬性,文本內容,文檔內容的處理.
***************相關代碼*******
//實例化一個DefaultHandler對象(StudentHandler是一事件處理器)
public class StudentHandler extends DefaultHandler {
private List<Student> mstudent;
private String mtagname;
private Student student;
public StudentHandler() {
super();
// TODO Auto-generated constructor stub
}
public StudentHandler(List<Student> mstudent) {
super();
this.mstudent = mstudent;
}
//開始解析文檔
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
//解析xml文檔結尾
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
//解析開始標籤
//元素節點
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
this.mtagname = localName;
if("student".equals(mtagname)){
student = new Student();
student .setId(Integer.valueOf(attributes.getValue("id")));
}
else if("age".equals(mtagname)){
student .setAge(Integer.valueOf(attributes.getValue(0)));
}
super.startElement(uri, localName, qName, attributes);
}
//解析標籤 結束
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
this.mtagname= localName;
if("student".equals(mtagname)){
mstudent.add(student);
student = null;
}
mtagname = "";
Log.i("TAG", "infor");
super.endElement(uri, localName, qName);
}
//解析文本標籤
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String name = new String (ch,start,length);
if(!name.trim().equals("")&&"name".equals(mtagname)){
student.setName(name);
}
Log.i("TAG", "infor");
super.characters(ch, start, length);
}
}
*****************************sax解析*************
//student是一個實體類,有name,age,參數和構造方法,toString輸出
public class SaxParserActivity extends Activity {
private TextView mtextview;
private ListView mlistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.domparser);
mtextview = (TextView) findViewById(R.id.textView1);
mlistview = (ListView) findViewById(R.id.listView1);
mtextview.setText("sax解析xml");
List<Student> list = parser();
ArrayAdapter<Student> adapter = new ArrayAdapter<Student>(this,android.R.layout.simple_expandable_list_item_1,list);
mlistview.setAdapter(adapter);
}
private List<Student> parser() {
List <Student> list = new ArrayList<Student>();
try {
// 獲取工廠
SAXParserFactory sax = SAXParserFactory.newInstance();
//獲取sax解析器
SAXParser sf = sax.newSAXParser();
//獲取事件源
XMLReader xmlreader = sf.getXMLReader();
//建立事件處理器
StudentHandler studenthandle = new StudentHandler(list);
// 將事件處理器設置到事件源
xmlreader.setContentHandler(studenthandle);
//觸發解析事件,讀取raw下面的文檔
xmlreader.parse(new InputSource(getResources().openRawResource(R.raw.student)));
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("TAG",""+e.getMessage());
}
return list;
}
} dom