歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android SAX解析XML

Android中有SAX、DOM及PULL等方式解析xml:

SAX是一種占用內存少且解析速度快的解析器,它采用的是事件啟動。官方推薦使用

以下是通過網上 獲取的天氣xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!-- published at 2011-12-14 14:55:05 -->  
  3. <Profiles>  
  4.     <Weather>  
  5.         <city>廣州</city>  
  6.         <status1>多雲</status1>  
  7.         <status2></status2>  
  8.         <figure1>duoyun</figure1>  
  9.         <figure2>yin</figure2>  
  10.         <direction1>無持續風向</direction1>  
  11.         <direction2>無持續風向</direction2>  
  12.         <power1>≤3</power1>  
  13.         <power2>≤3</power2>  
  14.         <temperature1>20</temperature1>  
  15.         <temperature2>12</temperature2>  
  16.         <ssd>0</ssd>  
  17.         <tgd1>21</tgd1>  
  18.         <tgd2>21</tgd2>  
  19.         <zwx>2</zwx>  
  20.         <ktk>5</ktk>  
  21.         <pollution>3</pollution>  
  22.         <xcz>4</xcz>  
  23.         <zho />  
  24.         <diy />  
  25.         <fas />  
  26.         <chy>4</chy>  
  27.         <zho_shuoming>暫無</zho_shuoming>  
  28.         <diy_shuoming>暫無</diy_shuoming>  
  29.         <fas_shuoming>暫無</fas_shuoming>  
  30.         <chy_shuoming>套裝、夾衣、風衣、夾克衫、西服套裝、馬甲襯衫配長褲</chy_shuoming>  
  31.         <pollution_l>一般</pollution_l>  
  32.         <zwx_l></zwx_l>  
  33.         <ssd_l>舒適</ssd_l>  
  34.         <fas_l>暫無</fas_l>  
  35.         <zho_l>暫無</zho_l>  
  36.         <chy_l>夾衣類</chy_l>  
  37.         <ktk_l>比較適宜(制熱)</ktk_l>  
  38.         <xcz_l>不太適宜</xcz_l>  
  39.         <diy_l>暫無</diy_l>  
  40.         <pollution_s>對空氣污染物擴散無明顯影響</pollution_s>  
  41.         <zwx_s>紫外線弱</zwx_s>  
  42.         <ssd_s>適宜在自然環境及露天場所活動。</ssd_s>  
  43.         <ktk_s>比較適宜開啟空調</ktk_s>  
  44.         <xcz_s>洗車後未來1-2天內有降水、大風或沙塵天氣,或洗車當日氣溫太低容易結冰。不太適宜洗車。</xcz_s>  
  45.         <gm>1</gm>  
  46.         <gm_l>低發期</gm_l>  
  47.         <gm_s>天氣舒適,不易發生感冒;</gm_s>  
  48.         <yd>2</yd>  
  49.         <yd_l>比較適宜</yd_l>  
  50.         <yd_s>雖然天空雲層較厚,比較適宜戶外運動;</yd_s>  
  51.         <savedate_weather>2011-12-14</savedate_weather>  
  52.         <savedate_life>2011-12-14</savedate_life>  
  53.         <savedate_zhishu>2011-12-14</savedate_zhishu>  
  54.     </Weather>  
  55. </Profiles>  

解析類:繼承DefaultHandler類

  1. package wu.lis.action;  
  2.   
  3. import org.xml.sax.Attributes;  
  4. import org.xml.sax.SAXException;  
  5. import org.xml.sax.helpers.DefaultHandler;  
  6.   
  7. import wu.lis.model.Weather;  
  8. import wu.lis.util.Constant;  
  9.   
  10. public class WeatherHandler extends DefaultHandler {  
  11.     private Weather weather;  
  12.     private String preTag = null;// 作用是記錄解析時的上一個節點名稱  
  13.   
  14.     @Override  
  15.     public void startDocument() throws SAXException {  
  16.         weather = new Weather();  
  17.         System.out.println("startDocument");  
  18.     }  
  19.   
  20.     @Override  
  21.     public void startElement(String uri, String localName, String qName,  
  22.             Attributes attributes) throws SAXException {  
  23.         preTag = qName;  
  24.         System.out.println(preTag);  
  25.     }  
  26.   
  27.     @Override  
  28.     public void characters(char[] ch, int start, int length)  
  29.             throws SAXException {  
  30.         String result = new String(ch, start, length);  
  31.   
  32.         if (preTag.equals(Constant.City)) {  
  33.             if (weather.getCity() == null)  
  34.                 weather.setCity(result);  
  35.         }  
  36.         if (preTag.equals(Constant.Status1)) {  
  37.             if (weather.getStatus1() == null)  
  38.                 weather.setStatus1(result);  
  39.         }  
  40.         if (preTag.equals(Constant.Status2)) {  
  41.             if (weather.getStatus2() == null)  
  42.                 weather.setStatus2(result);  
  43.         }  
  44.         if (preTag.equals(Constant.Figure1)) {  
  45.             if (weather.getFigure1() == null)  
  46.                 weather.setFigure1(result);  
  47.         }  
  48.         if (preTag.equals(Constant.Figure2)) {  
  49.             if (weather.getFigure2() == null)  
  50.                 weather.setFigure2(result);  
  51.         }  
  52.   
  53.         if (preTag.equals(Constant.Direction1)) {  
  54.             if (weather.getDirection1() == null)  
  55.                 weather.setDirection1(result);  
  56.         }  
  57.         if (preTag.equals(Constant.Direction2)) {  
  58.             if (weather.getDirection2() == null)  
  59.                 weather.setDirection2(result);  
  60.         }  
  61.   
  62.         if (preTag.equals(Constant.Power1)) {  
  63.             if (weather.getPower1() == null)  
  64.                 weather.setPower1(result);  
  65.         }  
  66.         if (preTag.equals(Constant.Power2)) {  
  67.             if (weather.getPower2() == null)  
  68.                 weather.setPower2(result);  
  69.         }  
  70.   
  71.         if (preTag.equals(Constant.Temperature1)) {  
  72.             System.out.println(preTag);  
  73.             if (weather.getTemperature1() == null)  
  74.                 weather.setTemperature1(result);  
  75.         }  
  76.         if (preTag.equals(Constant.Temperature2)) {  
  77.             if (weather.getTemperature2() == null)  
  78.                 weather.setTemperature2(result);  
  79.         }  
  80.   
  81.         if (preTag.equals(Constant.Ssd)) {  
  82.             if (weather.getSsd() == null)  
  83.                 weather.setSsd(result);  
  84.         }  
  85.         if (preTag.equals(Constant.Ssd_l)) {  
  86.             if (weather.getSsd_l() == null)  
  87.                 weather.setSsd_l(result);  
  88.         }  
  89.         if (preTag.equals(Constant.Ssd_s)) {  
  90.             if (weather.getSsd_s() == null)  
  91.                 weather.setSsd_s(result);  
  92.         }  
  93.   
  94.         if (preTag.equals(Constant.Tgd1)) {  
  95.             if (weather.getTgd1() == null)  
  96.                 weather.setTgd1(result);  
  97.         }  
  98.         if (preTag.equals(Constant.Tgd2)) {  
  99.             if (weather.getTgd2() == null)  
  100.                 weather.setTgd2(result);  
  101.         }  
  102.   
  103.         if (preTag.equals(Constant.Zwx)) {  
  104.             if (weather.getZwx() == null)  
  105.                 weather.setZwx(result);  
  106.         }  
  107.         if (preTag.equals(Constant.Zwx_l)) {  
  108.             if (weather.getZwx_l() == null)  
  109.                 weather.setZwx_l(result);  
  110.         }  
  111.         if (preTag.equals(Constant.Zwx_s)) {  
  112.             if (weather.getZwx_s() == null)  
  113.                 weather.setZwx_s(result);  
  114.         }  
  115.   
  116.         if (preTag.equals(Constant.Ktk)) {  
  117.             if (weather.getKtk() == null)  
  118.                 weather.setKtk(result);  
  119.         }  
  120.         if (preTag.equals(Constant.Ktk_l)) {  
  121.             if (weather.getKtk_l() == null)  
  122.                 weather.setKtk_l(result);  
  123.         }  
  124.         if (preTag.equals(Constant.Ktk_s)) {  
  125.             if (weather.getKtk_s() == null)  
  126.                 weather.setKtk_s(result);  
  127.         }  
  128.   
  129.         if (preTag.equals(Constant.Pollution))  
  130.             if (weather.getPollution() == null)  
  131.                 weather.setPollution(result);  
  132.         if (preTag.equals(Constant.Pollution_l))  
  133.             if (weather.getPollution_l() == null)  
  134.                 weather.setPollution_l(result);  
  135.         if (preTag.equals(Constant.Pollution_s))  
  136.             if (weather.getPollution_s() == null)  
  137.                 weather.setPollution_s(result);  
  138.   
  139.         if (preTag.equals(Constant.Xcz))  
  140.             if (weather.getXcz() == null)  
  141.                 weather.setXcz(result);  
  142.         if (preTag.equals(Constant.Xcz_l))  
  143.             if (weather.getXcz_l() == null)  
  144.                 weather.setXcz_l(result);  
  145.         if (preTag.equals(Constant.Xcz_s))  
  146.             if (weather.getXcz_s() == null)  
  147.                 weather.setXcz_s(result);  
  148.   
  149.         if (preTag.equals(Constant.Chy))  
  150.             if (weather.getChy() == null)  
  151.                 weather.setChy(result);  
  152.         if (preTag.equals(Constant.Chy_l))  
  153.             if (weather.getChy_l() == null)  
  154.                 weather.setChy_l(result);  
  155.         if (preTag.equals(Constant.Chy_shuoming))  
  156.             if (weather.getChy_shuoming() == null)  
  157.                 weather.setChy_shuoming(result);  
  158.   
  159.         if (preTag.equals(Constant.Gm))  
  160.             if (weather.getGm() == null)  
  161.                 weather.setGm(result);  
  162.         if (preTag.equals(Constant.Gm_l))  
  163.             if (weather.getGm_l() == null)  
  164.                 weather.setGm_l(result);  
  165.         if (preTag.equals(Constant.Gm_s))  
  166.             if (weather.getGm_s() == null)  
  167.                 weather.setGm_s(result);  
  168.   
  169.         if (preTag.equals(Constant.Yd))  
  170.             if (weather.getYd() == null)  
  171.                 weather.setYd(result);  
  172.         if (preTag.equals(Constant.Yd_l))  
  173.             if (weather.getYd_l() == null)  
  174.                 weather.setYd_l(result);  
  175.         if (preTag.equals(Constant.Yd_s))  
  176.             if (weather.getYd_s() == null)  
  177.                 weather.setYd_s(result);  
  178.   
  179.         if (preTag.equals(Constant.Savedate_weather))  
  180.             if (weather.getSavedate_weather() == null)  
  181.                 weather.setSavedate_weather(result);  
  182.         if (preTag.equals(Constant.Savedate_life))  
  183.             if (weather.getSavedate_life() == null)  
  184.                 weather.setSavedate_life(result);  
  185.         if (preTag.equals(Constant.Savedate_zhishu))  
  186.             if (weather.getSavedate_zhishu() == null)  
  187.                 weather.setSavedate_zhishu(result);  
  188.     }  
  189.   
  190.     @Override  
  191.     public void endElement(String uri, String localName, String qName)  
  192.             throws SAXException {  
  193.         if (qName.equals("Profiles")) {  
  194.             preTag = null;  
  195.         }  
  196.     }  
  197.   
  198.     @Override  
  199.     public void endDocument() throws SAXException {  
  200.         super.endDocument();  
  201.         System.out.println("城市:" + weather.getCity());  
  202.         System.out.println("白天:" + weather.getStatus1());  
  203.         System.out.println("夜間:" + weather.getStatus2());  
  204.         System.out.println("白天風向:"+weather.getDirection1());  
  205.         System.out.println("夜間風向:"+weather.getDirection2());  
  206.         System.out.println("白天溫度:" + weather.getTemperature1());  
  207.         System.out.println("夜間溫度:" + weather.getTemperature2());  
  208.     }  
  209. }  

調用解析

  1. InputStream inStream = getClass().getClassLoader().getResourceAsStream("weather.xml");  
  2.         System.out.println(result);  
  3.         SAXParserFactory factory = SAXParserFactory.newInstance();  
  4.         try {  
  5.             SAXParser parser = factory.newSAXParser();  
  6.             XMLReader reader = parser.getXMLReader();  
  7.             reader.setContentHandler(new WeatherHandler());  
  8.             reader.parse(new InputSource(inStream));  
  9.         } catch (Exception e) {  
  10.             e.printStackTrace();  
  11.         }  
Copyright © Linux教程網 All Rights Reserved