利用SAX、DOM和Pull實現對schools.xml的解析,並進行單元測試
利用Pull實現schools.xml的創建
源碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/12月/利用SAX、DOM和Pull實現對schools.xml的解析並進行單元測試/
首先給出schools.xml文件的代碼:
- <?xml version="1.0" encoding="UTF-8"?>
- <?xml-stylesheet type="text/css" href="school.css"?>
- <!DOCTYPE schools SYSTEM "schools.dtd">
- <schools>
-
- <school level="middle-school">
- <teacher>
- <teacherName>Tom</teacherName>
- <teacherAge>34</teacherAge>
- </teacher>
- <student>
- <studentName>Jack</studentName>
- <studentAge>28</studentAge>
- </student>
- <student>
- <studentName>Mary</studentName>
- <studentAge>22</studentAge>
- </student>
- <student>
- <studentName>John</studentName>
- <studentAge>23</studentAge>
- </student>
- </school>
-
- <school level="primary-school">
- <teacher>
- <teacherName>Tom</teacherName>
- <teacherAge>34</teacherAge>
- </teacher>
- <student>
- <studentName>Jack</studentName>
- <studentAge>21</studentAge>
- </student>
- <student>
- <studentName>Mary</studentName>
- <studentAge>25</studentAge>
- </student>
- <student>
- <studentName>Jhon</studentName>
- <studentAge>26</studentAge>
- </student>
-
- </school>
-
- </schools>
1、用sax解析該文件
SchooleHandler.java
- package cn.csdn.service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.xml.sax.Attributes;
- import org.xml.sax.SAXException;
- import org.xml.sax.helpers.DefaultHandler;
-
- import android.util.Log;
- import cn.csdn.domain.School;
- import cn.csdn.domain.Student;
- import cn.csdn.domain.Teacher;
-
- public class SchoolHandler extends DefaultHandler {
- List<School> schoolList=null;
- List<Teacher> teacherList=null;
- List<Student> studentList=null;
- School school=null;
- Teacher teacher=null;
- Student student=null;
- String elementTag = null;
-
- public void characters(char[] ch, int start, int length)
- throws SAXException {
- String data = new String(ch, start, length);
- if (elementTag != null&&teacher!=null) {
- if("teacherName".equals(elementTag)){
- teacher.setName(data);
- //Log.i("TAG",teacher.toString());
- }else if("teacherAge".equals(elementTag)){
- teacher.setAge(Integer.valueOf(data));
- //Log.i("TAG",teacher.toString());
- }
- }
- if(elementTag!=null&&student!=null){
- if("studentName".equals(elementTag)){
- student.setName(data);
- }else if("studentAge".equals(elementTag)){
- //Log.i("TAG",data);
- student.setAge(Integer.parseInt(data));
- }
- }
- }
-
- public void endDocument() throws SAXException {
- }
-
- public void endElement(String uri, String localName, String qName)
- throws SAXException {
- if("teacher".equals(localName)){
- //Log.i("TAG",teacher.toString());
- teacherList.add(teacher);
- school.setTeacher(teacher);
- teacher=null;
- }else if("student".equals(localName)){
- studentList.add(student);
- //school.setStudent(studentList);
- student=null;
- }else if("school".equals(localName)){
- school.setStudent(studentList);
- schoolList.add(school);
- studentList=null;
- school=null;
- }
- elementTag = null;
- }
-
- public void startDocument() throws SAXException {
- schoolList=new ArrayList<School>();
- teacherList=new ArrayList<Teacher>();
- studentList=new ArrayList<Student>();
- }
-
- public void startElement(String uri, String localName, String qName,
- Attributes attributes) throws SAXException {
- if("school".equals(localName)){
- school=new School();
- school.setLevel(attributes.getValue(0));
- studentList=new ArrayList<Student>();
- //Log.i("TAG",attributes.getValue(0));
- }else if("teacher".equals(localName)){
- teacher=new Teacher();
- //Log.i("TAG",teacher.toString());
- if(teacher!=null){
- // Log.i("TAG",teacher.toString());
- }
- }else if("student".equals(localName)){
- student=new Student();
- }
- elementTag = localName;
- }
-
- public List<School> getSchooList(){
- return schoolList;
- }
- }