利用SAX、DOM和Pull实现对schools.xml的解析并进行单元测试
利用SAX、DOM和Pull实现对schools.xml的解析,并进行单元测试
利用Pull实现schools.xml的创建
源码下载地址:
具体下载目录在 /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>
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;
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20