Android中使用SAX來解析XML
在Java中使用SAX來解析XML,這樣好像耗內(nèi)存會比DOM方式少些,適合于手持設(shè)備上的XML處理。
用SAX處理以下XML文件:
最后,得到城市的經(jīng)緯度坐標(biāo),可以用經(jīng)緯度來在Google Weather API中查詢天氣情況。這里有一個根據(jù)輸入的中文城市名來查詢天氣的Android實(shí)現(xiàn),是從《Android應(yīng)用開發(fā)揭秘》的示例程序改的。
點(diǎn)擊下載
用SAX處理以下XML文件:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<citys>
3
<city name="上海"><long>121480000</long><lat>31220000</lat></city>
4
<city name="嘉定"><long>121240000</long><lat>31400000</lat></city>
5
<city name="寶山"><long>121480000</long><lat>31410000</lat></city>
6
<city name="川沙"><long>121700000</long><lat>31190000</lat></city>
7
<city name="南匯"><long>121760000</long><lat>31050000</lat></city>
8
<city name="奉賢"><long>121460000</long><lat>30920000</lat></city>
9
<city name="松江"><long>121240000</long><lat>31000000</lat></city>
10
<city name="金山"><long>121160000</long><lat>30890000</lat></city>
11
<city name="青浦"><long>121100000</long><lat>31150000</lat></city>
12
<city name="崇明"><long>121400000</long><lat>31730000</lat></city>
13
</citys>
14
這里用查詢指定城市的坐標(biāo)信息:
2

3

4

5

6

7

8

9

10

11

12

13

14

1
package sax.test;
2
3
import java.io.IOException;
4
5
import javax.xml.parsers.SAXParser;
6
import javax.xml.parsers.SAXParserFactory;
7
8
import org.xml.sax.Attributes;
9
import org.xml.sax.InputSource;
10
import org.xml.sax.SAXException;
11
import org.xml.sax.XMLReader;
12
import org.xml.sax.helpers.DefaultHandler;
13
14
15
public class CityInfoHandler extends DefaultHandler {
16
private static CityInfoHandler instance = new CityInfoHandler();
17
private String tarName = "";
18
private String subTarName = "";
19
private String cityName = "";
20
private LatAndLong latAndLong = null;
21
22
23
public LatAndLong getLatAndLong() {
24
tarName = "";
25
cityName = "";
26
return latAndLong;
27
}
28
29
public void setCityName(String cityName) {
30
this.cityName = cityName;
31
}
32
33
@Override
34
public void startDocument() throws SAXException {
35
latAndLong = new LatAndLong();
36
}
37
38
@Override
39
public void startElement(String uri, String localName, String qName,
40
Attributes atts) throws SAXException {
41
System.out.println("qName = "+ qName);
42
System.out.println("localName = " + localName);
43
if ("city".equals(qName)) {
44
String cname = atts.getValue("name");
45
if(cname.equalsIgnoreCase(cityName)){
46
tarName = qName;
47
}
48
}
49
50
if("long".equals(qName) || "lat".equals(qName)){
51
subTarName = qName;
52
}
53
54
}
55
56
@Override
57
public void characters(char[] ch, int start, int length)
58
throws SAXException {
59
// 錕斤拷錕斤拷錕介到錕斤拷
60
if(tarName != "" && tarName.length() > 0 && tarName.equals("city")){
61
String text = new String(ch, start, length);
62
63
if("long".equals(subTarName)){
64
latAndLong.setAlong(text);
65
}else if("lat".equals(subTarName)){
66
latAndLong.setAlat(text);
67
}
68
69
if(latAndLong.isOk()){
70
tarName = "";
71
cityName = "";
72
}
73
74
}
75
76
subTarName = "";
77
78
}
79
80
81
82
public static LatAndLong queryLatAndLong(String cityName){
83
try {
84
instance.setCityName(cityName);
85
SAXParserFactory factory = SAXParserFactory.newInstance();
86
SAXParser parser = factory.newSAXParser();
87
XMLReader xr = parser.getXMLReader();
88
89
xr.setContentHandler(instance);
90
91
xr.parse(new InputSource(ClassLoader.getSystemResourceAsStream("city.xml")));
92
return instance.getLatAndLong();
93
} catch (IOException e) {
94
e.printStackTrace();
95
} catch (SAXException e) {
96
e.printStackTrace();
97
} catch (Exception e) {
98
e.printStackTrace();
99
}
100
101
return null;
102
103
}
104
105
public static void main(String[] args) {
106
LatAndLong ll = queryLatAndLong("上海");
107
System.out.println(ll.getAlat() + "," + ll.getAlong());
108
}
109
}
110

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

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

1
package sax.test;
2
3
/**
4
* 經(jīng)緯度
5
*
6
* @author vaga
7
*
8
*/
9
public class LatAndLong {
10
private String alat = null;
11
private String along = null;
12
13
private boolean isOk = false;
14
15
public boolean isOk() {
16
return isOk;
17
}
18
19
public String getAlat() {
20
return alat;
21
}
22
23
public void setAlat(String alat) {
24
this.alat = alat;
25
if(this.alat != null && this.along != null){
26
isOk = true;
27
}
28
}
29
30
public String getAlong() {
31
return along;
32
}
33
34
public void setAlong(String along) {
35
this.along = along;
36
if(this.alat != null && this.along != null){
37
isOk = true;
38
}
39
}
40
41
}
42
這里需要注意的是 public void startElement(String uri, String localName, String qName,Attributes atts) ,這個方法的參數(shù)localName和qName,localName是不加Namespace前綴的標(biāo)簽名,而qName是加Namespace前綴的,如果沒有指定Namespace,則qName可能為空,當(dāng)然不同的SAX實(shí)現(xiàn)會有所不同,比如在Android中qName為空,而J2SE中l(wèi)ocalName為空,所以想要總是得到標(biāo)簽名,就需要檢查這兩個參數(shù)的值了。
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

最后,得到城市的經(jīng)緯度坐標(biāo),可以用經(jīng)緯度來在Google Weather API中查詢天氣情況。這里有一個根據(jù)輸入的中文城市名來查詢天氣的Android實(shí)現(xiàn),是從《Android應(yīng)用開發(fā)揭秘》的示例程序改的。
點(diǎn)擊下載
posted on 2011-04-17 13:40 vagasnail 閱讀(2663) 評論(4) 編輯 收藏 所屬分類: java 、Android