android中使用SAX解析xml(封装)
SAX具体怎么用就不说了,网上搜搜一大片。
第一步:建立公共抽象类BaseHandler
package com.dc.test;import java.util.Vector;import junit.framework.TestCase;import com.dc.handler.BaseHandler;import com.dc.handler.StudentHandler;import com.dc.model.Student;public class TestStudentHandler extends TestCase {public void test1(){String data="<students><student sn='001'><id>1</id><name>benny</name><age>20</age></student><student sn='002'><id>2</id><name>jimmy</name><age>30</age></student></students>";BaseHandler handler=new StudentHandler(); handler.parse(data);Vector students=(Vector)BaseHandler.hash.get("students");//hash很重要,所有的东西都在hash中for(int i=0;i<students.size();i++){Student student=(Student)students.elementAt(i);if(student==null){System.out.println("student is null");}else{System.out.println(student.getSn()+":"+student.getId()+":"+student.getName()+":"+student.getAge());}}}}