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. }

  这个是执行结果,完全解析正确

相关内容