歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發:天氣預報Dom解析

Android開發:天氣預報Dom解析

日期:2017/3/1 10:48:02   编辑:Linux編程

DOM是用與平台無關和語言無關的方式表示XML文檔的官方W3C標准,DOM是以層次結構組織的節點或信息片段的集合。DOM是基於樹的,DOM相對SAX來說簡單,耗內存...

本次學習目標:了解DOM解析XML ,並用DOM解析谷歌提供的天氣

谷歌提供的天氣接口是 http://www.google.com/ig/api?hl=zh_CN&weather=wuhan 這個接口末尾是wuhan 即 "武漢" 的拼音,依次類推,北京的查詢方式是把後面拼音換成beijing就行了,這個接口是查詢武漢四天的天氣。

根元素(Element)是 xml_api_reply 即樹的根 然後往裡面擴展。


我要獲取節點forecas_conditions中的數據

DOM初始工作需要幾個函數

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new InputStreamReader(lianJie(strUrl) )));

然後通過Document對象解析XML,解析XML時會用到節點,並取得他的值 用到類 NodeList ,Node. 下面開始上我的程序

  1. package com.study.weather;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.Node;
  13. import org.w3c.dom.NodeList;
  14. import org.xml.sax.InputSource;
  15. import org.xml.sax.SAXException;
  16. public class Weather
  17. {
  18. public InputStream lianJie(String strUrl) throws IOException
  19. {
  20. URL url = new URL(strUrl);
  21. HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  22. InputStream is = urlConnection.getInputStream();
  23. if(is!=null)
  24. {
  25. return is;
  26. }
  27. return null;
  28. }
  29. public void resolutionXML(String strUrl) throws ParserConfigurationException, SAXException, IOException
  30. {
  31. WeatherData wd = new WeatherData();
  32. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  33. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  34. Document document = builder.parse(new InputSource(new InputStreamReader(lianJie(strUrl) )));
  35. // 得到xml_api_reply
  36. Element rootElement = document.getDocumentElement();
  37. System.out.println(rootElement.getNodeName());
  38. Node weatherNode = rootElement.getElementsByTagName("weather").item(0);
  39. // Node weatherNode = rootElement.getFirstChild();
  40. System.out.println(weatherNode.getNodeName());
  41. // 得到weather
  42. // Node nodeWeather = weatherNode.getChildNodes();
  43. // 得到weather下節點數組
  44. NodeList nodeForecastWeathers = weatherNode.getChildNodes();
  45. // 遍歷weather下的節點
  46. for(int i=0; i<nodeForecastWeathers.getLength(); i++)
  47. {
  48. Node nodeForecastWeather = nodeForecastWeathers.item(i);
  49. // 篩選節點 找名稱為 forecast_conditions 節點
  50. if(nodeForecastWeather.getNodeType()==Node.ELEMENT_NODE
  51. &&nodeForecastWeather.getNodeName().equals("forecast_conditions"))
  52. {
  53. // 建立forecast_conditions下節點數組
  54. NodeList nodeListForecastConditions = nodeForecastWeather.getChildNodes();
  55. for(int j=0; j<nodeListForecastConditions.getLength(); j++)
  56. {
  57. //day_of_week low high condition
  58. Node data = nodeListForecastConditions.item(j);
  59. if(data.getNodeName().equals("day_of_week"))
  60. {
  61. wd.setDayOfWeek(data.getAttributes().getNamedItem("data").getNodeValue());
  62. }
  63. else if(data.getNodeName().equals("low"))
  64. {
  65. wd.setLow(data.getAttributes().item(0).getNodeValue());
  66. }
  67. else if(data.getNodeName().equals("high"))
  68. {
  69. wd.setHigh(data.getAttributes().item(0).getNodeValue());
  70. }
  71. else if(data.getNodeName().equals("condition"))
  72. {
  73. wd.setConditionData(data.getAttributes().item(0).getNodeValue());
  74. }
  75. }
  76. System.out.println(wd.printWeatheaInfo());
  77. }
  78. }
  79. }
  80. public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
  81. {
  82. Weather weather = new Weather();
  83. weather.resolutionXML("http://www.google.com/ig/api?hl=zh_CN&weather=wuhan");
  84. }
  85. class WeatherData
  86. {
  87. String dayOfWeek;
  88. String low;
  89. String high;
  90. String conditionData;
  91. public void setDayOfWeek(String dayOfWeek)
  92. {
  93. this.dayOfWeek = dayOfWeek;
  94. }
  95. public void setLow(String low)
  96. {
  97. this.low = low;
  98. }
  99. public void setHigh(String high)
  100. {
  101. this.high = high;
  102. }
  103. public void setConditionData(String conditionData)
  104. {
  105. this.conditionData = conditionData;
  106. }
  107. public String printWeatheaInfo()
  108. {
  109. return dayOfWeek+"\n"+"溫度: "+low+"~~"+high+" \n天氣情況: "+conditionData;
  110. }
  111. }
  112. }

這個是執行結果,完全解析正確

Copyright © Linux教程網 All Rights Reserved