歡迎使用我的 在線工具

          小D

          讀歷史、看小說、寫程序都是我所愛。技術不好,頭腦不靈光,靠的是興趣。
          隨筆 - 35, 文章 - 25, 評論 - 13, 引用 - 0
          數據加載中……

          Android中使用SAX來解析XML

              在Java中使用SAX來解析XML,這樣好像耗內存會比DOM方式少些,適合于手持設備上的XML處理。
              用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
              這里用查詢指定城市的坐標信息:
             
            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
              
           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為空,所以想要總是得到標簽名,就需要檢查這兩個參數的值了。
               最后,得到城市的經緯度坐標,可以用經緯度來在Google Weather API中查詢天氣情況。這里有一個根據輸入的中文城市名來查詢天氣的Android實現,是從《Android應用開發揭秘》的示例程序改的。

          點擊下載

          posted on 2011-04-17 13:40 vagasnail 閱讀(2663) 評論(4)  編輯  收藏 所屬分類: java 、Android

          評論

          # re: Android中使用SAX來解析XML  回復  更多評論   

          getSystemResourceAsStream() 這個里面的city.xml是放在那里的?謝謝。
          2012-02-12 12:03 | daidi

          # re: Android中使用SAX來解析XML  回復  更多評論   

          @daidi
          assets目錄下面的
          2012-02-12 18:22 | vagasnail

          # re: Android中使用SAX來解析XML  回復  更多評論   

          代碼中不寫點注釋和費了沒什么區別!!
          2013-08-27 22:49 | 小小菜

          # re: Android中使用SAX來解析XML  回復  更多評論   

          @小小菜
          本來無所謂廢了就廢了,而且這么簡單,而且我也加了兩條注釋
          2013-09-05 11:38 | vagasnail
          主站蜘蛛池模板: 云林县| 五家渠市| 定陶县| 双牌县| 定州市| 滦南县| 扶风县| 固原市| 中牟县| 平罗县| 永宁县| 平遥县| 武义县| 宜良县| 通道| 房产| 时尚| 牙克石市| 清水河县| 开封县| 濉溪县| 当涂县| 运城市| 西贡区| 留坝县| 来宾市| 新丰县| 东方市| 含山县| 宁河县| 达州市| 中阳县| 从江县| 沈阳市| 昌邑市| 新平| 翼城县| 大英县| 南充市| 永昌县| 玛沁县|