Advertisement
Guest User

Using Google Weather API in Java Code

a guest
Nov 5th, 2011
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. // Mainly putting this here in case someone searches like I did for some code to parse the google weather API
  2.  
  3. // file.xml is a sample file, just copy/paste output from google weather api into text file and save
  4.  
  5. // Enjoy.
  6.  
  7. package googleweatherapi;
  8.  
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.net.URL;
  12. import java.net.URLConnection;
  13. import javax.xml.parsers.DocumentBuilder;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import org.w3c.dom.Document;
  17. import org.w3c.dom.Element;
  18. import org.w3c.dom.Node;
  19. import org.w3c.dom.NodeList;
  20. import org.xml.sax.SAXException;
  21.  
  22. public class GoogleWeatherAPI {
  23.  
  24.     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
  25.  
  26.        
  27.         File file = new File("file.xml");
  28.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  29.         DocumentBuilder db = dbf.newDocumentBuilder();
  30.         Document doc = db.parse(file);
  31.         doc.getDocumentElement().normalize();
  32.         System.out.println("Root element " + doc.getDocumentElement().getNodeName());
  33.  
  34.         // Current Conditions:
  35.  
  36.         // Get City
  37.         NodeList nodeLst = doc.getElementsByTagName("city");
  38.         Element birthTime = (Element) nodeLst.item(0);
  39.         String value = birthTime.getAttribute("data");
  40.         System.out.println(value);
  41.  
  42.         // Get Condition
  43.         nodeLst = doc.getElementsByTagName("condition");
  44.         Element curCond = (Element) nodeLst.item(0);
  45.         String curCondStr = curCond.getAttribute("data");
  46.         System.out.println(curCondStr);
  47.  
  48.         // Get Humidity
  49.         nodeLst = doc.getElementsByTagName("humidity");
  50.         Element curHumidity = (Element) nodeLst.item(0);
  51.         String curHumidityStr = curHumidity.getAttribute("data");
  52.         System.out.println(curHumidityStr);
  53.  
  54.         // Get Temp F
  55.         nodeLst = doc.getElementsByTagName("temp_f");
  56.         Element curTempF = (Element) nodeLst.item(0);
  57.         String curTempFStr = curTempF.getAttribute("data");
  58.         System.out.println("Degrees F: " + curTempFStr);
  59.  
  60.         // Get Temp C
  61.         nodeLst = doc.getElementsByTagName("temp_c");
  62.         Element curTempC = (Element) nodeLst.item(0);
  63.         String curTempCStr = curTempC.getAttribute("data");
  64.         System.out.println("Degrees C: " + curTempCStr);
  65.  
  66.         // end current conditions
  67.  
  68.         // future forecase:
  69.  
  70.         NodeList listOfForcasts = doc.getElementsByTagName("forecast_conditions");
  71.         int totalDaysForecasted = listOfForcasts.getLength();
  72.         System.out.println("Total number of days forecasted: " + totalDaysForecasted);
  73.  
  74.         for (int s = 0; s < listOfForcasts.getLength(); s++) {
  75.  
  76.  
  77.             Node fourDayForcastNode = listOfForcasts.item(s);
  78.             if (fourDayForcastNode.getNodeType() == Node.ELEMENT_NODE) {
  79.  
  80.                 Element dayOfWeekElement = (Element) fourDayForcastNode;
  81.                 //-------
  82.                 NodeList dayOfWeekList = dayOfWeekElement.getElementsByTagName("day_of_week");
  83.                 dayOfWeekElement = (Element) dayOfWeekList.item(0);
  84.                 String dayOfWeekStr = dayOfWeekElement.getAttribute("data");
  85.                 System.out.println("Day of week: " + dayOfWeekStr);
  86.  
  87.                 Element lowTempFElement = (Element) fourDayForcastNode;
  88.                 //-------
  89.                 NodeList lowTempFList = lowTempFElement.getElementsByTagName("low");
  90.                 lowTempFElement = (Element) lowTempFList.item(0);
  91.                 String lowTempFStr = lowTempFElement.getAttribute("data");
  92.                 System.out.println("Low temp F: " + lowTempFStr);
  93.  
  94.                 Element highTempFElement = (Element) fourDayForcastNode;
  95.                 //-------
  96.                 NodeList highTempFList = highTempFElement.getElementsByTagName("high");
  97.                 highTempFElement = (Element) highTempFList.item(0);
  98.                 String highTempFStr = highTempFElement.getAttribute("data");
  99.                 System.out.println("High temp F: " + highTempFStr);
  100.  
  101.                 Element conditionElement = (Element) fourDayForcastNode;
  102.                 //-------
  103.                 NodeList conditionList = conditionElement.getElementsByTagName("condition");
  104.                 conditionElement = (Element) conditionList.item(0);
  105.                 String conditionStr = conditionElement.getAttribute("data");
  106.                 System.out.println("Condition: " + conditionStr);
  107.             }
  108.             // End Future Forecast
  109.         }
  110.     }
  111. }
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement