歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android SAX解析XML

Android SAX解析XML

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

調用解析

  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