歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

利用SAX、DOM和Pull實現對schools.xml的解析並進行單元測試

利用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文件的代碼:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <?xml-stylesheet type="text/css" href="school.css"?>  
  3. <!DOCTYPE schools SYSTEM "schools.dtd">  
  4. <schools>  
  5.   
  6. <school level="middle-school">  
  7.     <teacher>  
  8.         <teacherName>Tom</teacherName>  
  9.         <teacherAge>34</teacherAge>  
  10.     </teacher>  
  11.     <student>  
  12.         <studentName>Jack</studentName>  
  13.         <studentAge>28</studentAge>  
  14.     </student>  
  15.     <student>  
  16.         <studentName>Mary</studentName>  
  17.         <studentAge>22</studentAge>  
  18.     </student>  
  19.     <student>  
  20.         <studentName>John</studentName>  
  21.         <studentAge>23</studentAge>  
  22.     </student>  
  23. </school>  
  24.   
  25. <school level="primary-school">  
  26.     <teacher>  
  27.         <teacherName>Tom</teacherName>  
  28.         <teacherAge>34</teacherAge>  
  29.     </teacher>  
  30.     <student>  
  31.         <studentName>Jack</studentName>  
  32.         <studentAge>21</studentAge>  
  33.     </student>  
  34.     <student>  
  35.         <studentName>Mary</studentName>  
  36.         <studentAge>25</studentAge>  
  37.     </student>  
  38.     <student>  
  39.         <studentName>Jhon</studentName>  
  40.         <studentAge>26</studentAge>  
  41.     </student>  
  42.   
  43. </school>  
  44.   
  45. </schools>  
1、用sax解析該文件

SchooleHandler.java

  1. package cn.csdn.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.xml.sax.Attributes;  
  7. import org.xml.sax.SAXException;  
  8. import org.xml.sax.helpers.DefaultHandler;  
  9.   
  10. import android.util.Log;  
  11. import cn.csdn.domain.School;  
  12. import cn.csdn.domain.Student;  
  13. import cn.csdn.domain.Teacher;  
  14.   
  15. public class SchoolHandler extends DefaultHandler {  
  16.     List<School> schoolList=null;  
  17.     List<Teacher> teacherList=null;  
  18.     List<Student> studentList=null;  
  19.     School school=null;  
  20.     Teacher teacher=null;  
  21.     Student student=null;  
  22.     String elementTag = null;  
  23.       
  24.     public void characters(char[] ch, int start, int length)  
  25.             throws SAXException {  
  26.         String data = new String(ch, start, length);  
  27.         if (elementTag != null&&teacher!=null) {  
  28.             if("teacherName".equals(elementTag)){  
  29.                 teacher.setName(data);  
  30.                 //Log.i("TAG",teacher.toString());   
  31.             }else if("teacherAge".equals(elementTag)){  
  32.                 teacher.setAge(Integer.valueOf(data));  
  33.                 //Log.i("TAG",teacher.toString());   
  34.             }  
  35.         }  
  36.         if(elementTag!=null&&student!=null){  
  37.             if("studentName".equals(elementTag)){  
  38.                 student.setName(data);  
  39.             }else if("studentAge".equals(elementTag)){  
  40.                 //Log.i("TAG",data);   
  41.                 student.setAge(Integer.parseInt(data));  
  42.             }  
  43.         }  
  44.     }  
  45.   
  46.     public void endDocument() throws SAXException {  
  47.     }  
  48.   
  49.     public void endElement(String uri, String localName, String qName)  
  50.             throws SAXException {  
  51.         if("teacher".equals(localName)){  
  52.             //Log.i("TAG",teacher.toString());   
  53.             teacherList.add(teacher);  
  54.             school.setTeacher(teacher);  
  55.             teacher=null;  
  56.         }else if("student".equals(localName)){  
  57.             studentList.add(student);  
  58.             //school.setStudent(studentList);   
  59.             student=null;  
  60.         }else if("school".equals(localName)){  
  61.             school.setStudent(studentList);  
  62.             schoolList.add(school);  
  63.             studentList=null;  
  64.             school=null;  
  65.         }  
  66.         elementTag = null;  
  67.     }  
  68.   
  69.     public void startDocument() throws SAXException {  
  70.         schoolList=new ArrayList<School>();  
  71.         teacherList=new ArrayList<Teacher>();  
  72.         studentList=new ArrayList<Student>();  
  73.     }  
  74.   
  75.     public void startElement(String uri, String localName, String qName,  
  76.             Attributes attributes) throws SAXException {  
  77.         if("school".equals(localName)){  
  78.             school=new School();  
  79.             school.setLevel(attributes.getValue(0));  
  80.             studentList=new ArrayList<Student>();  
  81.             //Log.i("TAG",attributes.getValue(0));   
  82.         }else if("teacher".equals(localName)){  
  83.             teacher=new Teacher();  
  84.             //Log.i("TAG",teacher.toString());   
  85.             if(teacher!=null){  
  86.             //  Log.i("TAG",teacher.toString());   
  87.             }  
  88.         }else if("student".equals(localName)){  
  89.             student=new Student();  
  90.         }  
  91.         elementTag = localName;  
  92.     }  
  93.   
  94.     public List<School> getSchooList(){  
  95.         return schoolList;  
  96.     }  
  97. }  
Copyright © Linux教程網 All Rights Reserved