Android使用SAX解析XML(2)

school类包含了一个major列表,可以增加该列表的元素,以及返回该列表,还实现了Parcelable.Creator接口。

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.hzhi.my_sax;
 
import java.util.ArrayList;
 
import android.os.Parcel;
import android.os.Parcelable;
 
public class school implements Parcelable{
   
    public static final String tag_name = "School";
   
    public String name;
    public String code;
    public ArrayList<major> majors;
   
    public static final Parcelable.Creator<school> CREATOR =
            new Parcelable.Creator<school>(){
 
                @Override
                public school createFromParcel(Parcel in) {
                    // TODO Auto-generated method stub
                    return new school(in);
                }
 
                @Override
                public school[] newArray(int size) {
                    // TODO Auto-generated method stub
                    return new school[size];
                }
               
            };
 
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        // TODO Auto-generated method stub
       
    }
   
    // 实现Parcelable接口
    public school(Parcel in){
       
        this.name = in.readString();
        this.code = in.readString();
       
    }
   
    // 构造函数
    public school(String name, String code){
       
        this.name = name;
        this.code = code;
        this.majors = new ArrayList<major>();
       
    }
   
    // 增加专业
    public void add_major(major m){
       
        this.majors.add(m);
       
    }
   
    // 返回专业
    public ArrayList<major> get_majors(){
        return this.majors;
    }
   
    // 重写toString
    public String toString() {
        // TODO Auto-generated method stub
        return (getName());
    }
   
    public String getName() {
       
        if(this.name == null) {
            return ("(Default)");
        }
 
        return (this.name);
       
    }
 
}

major类包含了一个clas列表,可以增加该列表的元素,以及返回该列表,也实现了Parcelable.Creator接口。

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.hzhi.my_sax;
 
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
 
public class major implements Parcelable{
   
    public static final String tag_name = "Major";
   
    public String name;
    public String code;
    public ArrayList<clas> clases;
   
   
    public static final Parcelable.Creator<major> CREATOR =
            new Parcelable.Creator<major>(){
 
                @Override
                public major createFromParcel(Parcel in) {
                    // TODO Auto-generated method stub
                    return new major(in);
                }
 
                @Override
                public major[] newArray(int size) {
                    // TODO Auto-generated method stub
                    return new major[size];
                }
               
            };
 
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
       
        // TODO Auto-generated method stub
        dest.writeString(this.name);
        dest.writeString(this.code);
       
    }
   
    // 实现Parcelable接口
    public major (Parcel in){
       
        this.name = in.readString();
        this.code = in.readString();
       
    }
   
   
    // 构造函数
    public major(String name, String code){
       
        this.name = name;
        this.code = code;
        this.clases = new ArrayList<clas>();
       
    }
   
    public String getCode() {
        return (this.code);
    }
   
    public void add_clas(clas c) {
        this.clases.add(c);
    }
 
    public ArrayList<clas> get_clases() {
        return this.clases;
    }  
   
    // 重写toString
    public String toString() {
        // TODO Auto-generated method stub
        return (getName());
    }
   
    public String getName() {
       
        if(this.name == null) {
            return ("(Default)");
        }
 
        return (this.name);
       
    }
 
}

clas类就不用包含列表了,但还是需要实现Parcelable.Creator接口。

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.hzhi.my_sax;
 
import android.os.Parcel;
import android.os.Parcelable;
 
public class clas implements Parcelable {
   
    public static final String tag_name = "Class";
   
    public String name;
    public String code;
   
    public static final Parcelable.Creator<clas> CREATOR =
            new Parcelable.Creator<clas>(){
 
                @Override
                public clas createFromParcel(Parcel in) {
                    // TODO Auto-generated method stub
                    return new clas(in);
                }
 
                @Override
                public clas[] newArray(int size) {
                    // TODO Auto-generated method stub
                    return new clas[size];
                }
               
            };
 
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
       
        // TODO Auto-generated method stub
        dest.writeString(this.name);
        dest.writeString(this.code);
       
    }
   
    public clas(Parcel in){
       
        this.name = in.readString();
        this.code = in.readString();
       
    }
   
    public clas (final String name, final String code){
       
        this.name = name;
        this.code = code;
       
    }
   
    // 重写toString
    public String toString() {
        // TODO Auto-generated method stub
        return (getName());
    }  
   
    public String getName() {
       
        if(this.name == null) {
            return ("(Default)");
        }
 
        return (this.name);
       
    }
 
}

以上4个类中,tag_name是为了和XML文件中的节点名称对比,name和code分别对应XML文件中的name和code。4个类都要重写toString()方法,否则返回的就不是数据内容,而是一长串数字。

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