当前位置:编程学习 > XML/UML >>

android应用开发之利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试

首先我们有如下的xml文件,我要对其进行解析
school.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>20</studentAge> 
    </student> 
    <student> 
        <studentName>Mary</studentName> 
        <studentAge>20</studentAge> 
    </student> 
    <student> 
        <studentName>John</studentName> 
        <studentAge>20</studentAge> 
    </student> 
</school> 
 
<school level="primary-school"> 
    <teacher> 
        <teacherName>Tom</teacherName> 
        <teacherAge>34</teacherAge> 
    </teacher> 
    <student> 
        <studentName>Jack</studentName> 
        <studentAge>20</studentAge> 
    </student> 
    <student> 
        <studentName>Mary</studentName> 
        <studentAge>20</studentAge> 
    </student> 
    <student> 
        <studentName>Jhon</studentName> 
        <studentAge>20</studentAge> 
    </student> 
 
</school> 
 
</schools> 

我们现在要对其进行解析,首先要分析他的元素组成部分
大家可以看到在根节点school中,含有如下的属性
1.level 2.子节点techer3.子节点student
创建javabean文件 www.zzzyk.com
现在我们创建school.java 的bean文件

package song.domain; 
 
import java.util.List; 
 
public class School { 
    private String level; 
    private Teacher teacher; 
    private List<Student> student; 
    public School() { 
        super(); 
        // TODO Auto-generated constructor stub 
    } 
    public School(String level, Teacher teacher, List<Student> student) { 
        super(); 
        this.level = level; 
        this.teacher = teacher; 
        this.student = student; 
    } 
    public String getLevel() { 
        return level; 
    } 
    public void setLevel(String level) { 
        this.level = level; 
    } 
    public Teacher getTeacher() { 
        return teacher; 
    } 
    public void setTeacher(Teacher teacher) { 
        this.teacher = teacher; 
    } 
    public List<Student> getStudent() { 
        return student; 
    } 
    public void setStudent(List<Student> student) { 
        this.student = student; 
    } 
    @Override 
    public String toString() { 
        return "Schoool [level=" + level + ", teacher=" + teacher 
                + ", student=" + student + "]"; 
    } 
     

我们又可以注意到,子节点techer的属性
1.teacherName2.techerAge
现在我们创建techer.java 的bean文件

package song.domain; 
 
public class Teacher { 
    private String teacherName; 
    private Integer teacherAge; 
    public Teacher() { 
        super(); 
        // TODO Auto-generated constructor stub 
    } 
    public Teacher(String teacherName, Integer teacherAge) { 
        super(); 
        this.teacherName = teacherName; 
        this.teacherAge = teacherAge; 
    } 
    public String getTeacherName() { 
        return teacherName; 
    } 
    public void setTeacherName(String teacherName) { 
        this.teacherName = teacherName; 
    } 
    public Integer getTeacherAge() { 
        return teacherAge; 
    } 
    public void setTeacherAge(Integer teacherAge) { 
        this.teacherAge = teacherAge; 
    } 
    @Override 
    public String toString() { 
        return "Teacher [teacherName=" + teacherName + ", teacherAge=" 
                + teacherAge + "]"; 
    } 
     

子节点student的属性
1.studentName2.studentAge
现在我们创建student的java

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,