Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Mainly putting this here in case someone searches like I did for some code to parse the google weather API
- // file.xml is a sample file, just copy/paste output from google weather api into text file and save
- // Enjoy.
- package googleweatherapi;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.net.URLConnection;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class GoogleWeatherAPI {
- public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
- File file = new File("file.xml");
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(file);
- doc.getDocumentElement().normalize();
- System.out.println("Root element " + doc.getDocumentElement().getNodeName());
- // Current Conditions:
- // Get City
- NodeList nodeLst = doc.getElementsByTagName("city");
- Element birthTime = (Element) nodeLst.item(0);
- String value = birthTime.getAttribute("data");
- System.out.println(value);
- // Get Condition
- nodeLst = doc.getElementsByTagName("condition");
- Element curCond = (Element) nodeLst.item(0);
- String curCondStr = curCond.getAttribute("data");
- System.out.println(curCondStr);
- // Get Humidity
- nodeLst = doc.getElementsByTagName("humidity");
- Element curHumidity = (Element) nodeLst.item(0);
- String curHumidityStr = curHumidity.getAttribute("data");
- System.out.println(curHumidityStr);
- // Get Temp F
- nodeLst = doc.getElementsByTagName("temp_f");
- Element curTempF = (Element) nodeLst.item(0);
- String curTempFStr = curTempF.getAttribute("data");
- System.out.println("Degrees F: " + curTempFStr);
- // Get Temp C
- nodeLst = doc.getElementsByTagName("temp_c");
- Element curTempC = (Element) nodeLst.item(0);
- String curTempCStr = curTempC.getAttribute("data");
- System.out.println("Degrees C: " + curTempCStr);
- // end current conditions
- // future forecase:
- NodeList listOfForcasts = doc.getElementsByTagName("forecast_conditions");
- int totalDaysForecasted = listOfForcasts.getLength();
- System.out.println("Total number of days forecasted: " + totalDaysForecasted);
- for (int s = 0; s < listOfForcasts.getLength(); s++) {
- Node fourDayForcastNode = listOfForcasts.item(s);
- if (fourDayForcastNode.getNodeType() == Node.ELEMENT_NODE) {
- Element dayOfWeekElement = (Element) fourDayForcastNode;
- //-------
- NodeList dayOfWeekList = dayOfWeekElement.getElementsByTagName("day_of_week");
- dayOfWeekElement = (Element) dayOfWeekList.item(0);
- String dayOfWeekStr = dayOfWeekElement.getAttribute("data");
- System.out.println("Day of week: " + dayOfWeekStr);
- Element lowTempFElement = (Element) fourDayForcastNode;
- //-------
- NodeList lowTempFList = lowTempFElement.getElementsByTagName("low");
- lowTempFElement = (Element) lowTempFList.item(0);
- String lowTempFStr = lowTempFElement.getAttribute("data");
- System.out.println("Low temp F: " + lowTempFStr);
- Element highTempFElement = (Element) fourDayForcastNode;
- //-------
- NodeList highTempFList = highTempFElement.getElementsByTagName("high");
- highTempFElement = (Element) highTempFList.item(0);
- String highTempFStr = highTempFElement.getAttribute("data");
- System.out.println("High temp F: " + highTempFStr);
- Element conditionElement = (Element) fourDayForcastNode;
- //-------
- NodeList conditionList = conditionElement.getElementsByTagName("condition");
- conditionElement = (Element) conditionList.item(0);
- String conditionStr = conditionElement.getAttribute("data");
- System.out.println("Condition: " + conditionStr);
- }
- // End Future Forecast
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement