歡迎使用我的 在線工具

          小D

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

          Android中使用SAX來解析XML

              在Java中使用SAX來解析XML,這樣好像耗內(nèi)存會比DOM方式少些,適合于手持設(shè)備上的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
              這里用查詢指定城市的坐標(biāo)信息:
             
            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 * 經(jīng)緯度
           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) ,這個方法的參數(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ù)的值了。
               最后,得到城市的經(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)  編輯  收藏 所屬分類: javaAndroid

          評論

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

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

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

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

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

          代碼中不寫點(diǎn)注釋和費(fèi)了沒什么區(qū)別!!
          2013-08-27 22:49 | 小小菜

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

          @小小菜
          本來無所謂廢了就廢了,而且這么簡單,而且我也加了兩條注釋
          2013-09-05 11:38 | vagasnail
          主站蜘蛛池模板: 清苑县| 交城县| 萍乡市| 东明县| 武宁县| 绵竹市| 忻州市| 景德镇市| 绥宁县| 巴塘县| 班戈县| 雷州市| 蒲江县| 嘉义县| 宁蒗| 阳西县| 商河县| 壶关县| 务川| 隆回县| 鄂伦春自治旗| 遵化市| 康乐县| 界首市| 云林县| 涡阳县| 万源市| 嘉定区| 洛南县| 沈阳市| 高安市| 富源县| 平潭县| 仁怀市| 兴义市| 米易县| 师宗县| 大竹县| 新田县| 海南省| 阳东县|