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

Android開發:天氣預報Dom解析

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

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

Copyright © Linux教程網 All Rights Reserved