Android使用SAX解析XML(5)

parse_handler.java文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.hzhi.my_sax;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class parse_handler extends DefaultHandler{
   
    private infor m_infor;
    private school m_c_school;
    private major m_c_major;
   
    public parse_handler(infor i){
        m_infor = i;
    }
   
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException{
       
        super.startElement(uri, localName, name, attributes);
       
        // 当前元素学院
        if (localName.equalsIgnoreCase(school.tag_name)){
           
            String school_name = attributes.getValue("Name");
            String code = attributes.getValue("Code");
           
            school m_school = new school(school_name, code);
            m_infor.add_school(m_school);
            // 记录当前的学院
            m_c_school = m_school;
           
        }
        // 当前元素有专业
        else if (localName.equalsIgnoreCase(major.tag_name)){
           
            String major_name = attributes.getValue("Name");
            String code = attributes.getValue("Code");
           
            major m_major = new major(major_name, code);
            m_c_school.add_major(m_major);
            // 记录当前的学院
            m_c_major = m_major;
                       
        }
        // 当前元素有班级
        else if (localName.equalsIgnoreCase(clas.tag_name)){
            String clas_name = attributes.getValue("Name");
            String code = attributes.getValue("Code");
 
            //如果其从属的专业不为空则添加其专业的班级列表
            if(m_c_major != null) {
                clas m_clas = new clas(clas_name, code);
                m_c_major.add_clas(m_clas);
            }
        }
       
       
    }
 
}

该类的startElement()函数具体的完成了对XML文件的解析过程,并将解析结果放入变量m_infor中。

posted @ 2013-12-31 17:59  萧萧  阅读(132)  评论(0)    收藏  举报