Android中使用SAX来解析XML
来源:互联网 发布:炒房洪流中的数据后门 编辑:程序博客网 时间:2024/05/16 08:17
在Java中使用SAX来解析XML,这样好像耗内存会比DOM方式少些,适合于手持设备上的XML处理。
用SAX处理以下XML文件:
最后,得到城市的经纬度坐标,可以用经纬度来在Google Weather API中查询天气情况。这里有一个根据输入的中文城市名来查询天气的Android实现,是从《Android应用开发揭秘》的示例程序改的。
用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
这里用查询指定城市的坐标信息: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
1package sax.test;
2
3import java.io.IOException;
4
5import javax.xml.parsers.SAXParser;
6import javax.xml.parsers.SAXParserFactory;
7
8import org.xml.sax.Attributes;
9import org.xml.sax.InputSource;
10import org.xml.sax.SAXException;
11import org.xml.sax.XMLReader;
12import org.xml.sax.helpers.DefaultHandler;
13
14
15public 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
3import java.io.IOException;
4
5import javax.xml.parsers.SAXParser;
6import javax.xml.parsers.SAXParserFactory;
7
8import org.xml.sax.Attributes;
9import org.xml.sax.InputSource;
10import org.xml.sax.SAXException;
11import org.xml.sax.XMLReader;
12import org.xml.sax.helpers.DefaultHandler;
13
14
15public 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
1package sax.test;
2
3/** *//**
4 * 经纬度
5 *
6 * @author vaga
7 *
8 */
9public 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) ,这个方法的参数localName和qName,localName是不加Namespace前缀的标签名,而qName是加Namespace前缀的,如果没有指定Namespace,则qName可能为空,当然不同的SAX实现会有所不同,比如在Android中qName为空,而J2SE中localName为空,所以想要总是得到标签名,就需要检查这两个参数的值了。2
3/** *//**
4 * 经纬度
5 *
6 * @author vaga
7 *
8 */
9public 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
最后,得到城市的经纬度坐标,可以用经纬度来在Google Weather API中查询天气情况。这里有一个根据输入的中文城市名来查询天气的Android实现,是从《Android应用开发揭秘》的示例程序改的。
- Android中使用SAX来解析XML
- Android中使用SAX来解析XML
- 使用 SAX 来解析XML
- Android中使用SAX解析XML
- android中使用sax解析xml
- Android中使用sax解析XML
- android中使用SAX解析xml文件
- Android中使用SAX解析XML
- Android中SAX解析XML的使用
- android中使用SAX, DOM, 或者pull 来解析XML文档
- Android中SAX解析XML
- Android中SAX解析XML
- Android中SAX解析XML
- Android中SAX解析XML
- android 使用SAX解析xml
- Android使用SAX解析XML
- android使用sax解析xml
- Android 使用SAX解析XML
- Foxmail收取邮件
- 投票协议:二进制表示方法
- 企业部署Windows 8 Store 风格应用
- HTML5 文件操作API
- Android Fragments 详细使用
- Android中使用SAX来解析XML
- Android 使用AudioRecord 录音只有噪音
- 黑马程序员_Map集合
- c++内存池
- 设计模式19:职责链模式【行为型】
- C语言程序设计语言统计字符出现的次数
- time_t 与字符串之间的相互转换
- 游戏开发应该掌握的知识
- 投票协议:发送和接收