aeroswat Posted July 14, 2010 Share Posted July 14, 2010 So I've never worked with XML before and I want to use the google API's. I've been doing quite a bit of scouting and this is what I've seen two different ways of doing things for the most part but neither are working for me Here is the code i was using $objDOM = new DOMDocument(); $objDOM->loadXML('http://www.google.com/ig/api?weather=New%20York'); $weather = $objDOM->getElementsByTagName("current_conditions"); foreach( $weather as $value ) { $dows = $value->getElementsByTagName("day_of_week"); $dow = $dows->item(0)->nodeValue; $lows = $value->getElementsByTagName("low"); $low = $lows->item(0)->nodeValue; $highs = $value->getElementsByTagName("highs"); $high = $highs->item(0)->nodeValue; $conditions = $value->getElementsByTagName("condition"); $condition = $conditions->item(0)->nodeValue; $pics = $value->getElementsByTagName("icon"); $pic = $pics->item(0)->nodeValue; echo "Day of Week: $dow <br>"; echo "Low: $low <br>"; echo "High: $high <br>"; echo "Condition: $condition <br>"; echo "<img src='www.google.com" . $pic . "' /><br /><br />"; } Can someone help me with what I'm doing wrong? It's not outputting anything but the labels When I go to that address this is what prints out <xml_api_reply version="1"> − <weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"> − <forecast_information> <city data="New York, NY"/> <postal_code data="New York"/> <latitude_e6 data=""/> <longitude_e6 data=""/> <forecast_date data="2010-07-14"/> <current_date_time data="2010-07-14 22:11:12 +0000"/> <unit_system data="US"/> </forecast_information> − <current_conditions> <condition data="Cloudy"/> <temp_f data="77"/> <temp_c data="25"/> <humidity data="Humidity: 80%"/> <icon data="/ig/images/weather/cloudy.gif"/> <wind_condition data="Wind: N at 8 mph"/> </current_conditions> − <forecast_conditions> <day_of_week data="Wed"/> <low data="72"/> <high data="79"/> <icon data="/ig/images/weather/chance_of_storm.gif"/> <condition data="Scattered Thunderstorms"/> </forecast_conditions> − <forecast_conditions> <day_of_week data="Thu"/> <low data="74"/> <high data="89"/> <icon data="/ig/images/weather/partly_cloudy.gif"/> <condition data="Partly Cloudy"/> </forecast_conditions> − <forecast_conditions> <day_of_week data="Fri"/> <low data="78"/> <high data="93"/> <icon data="/ig/images/weather/partly_cloudy.gif"/> <condition data="Partly Cloudy"/> </forecast_conditions> − <forecast_conditions> <day_of_week data="Sat"/> <low data="76"/> <high data="92"/> <icon data="/ig/images/weather/chance_of_storm.gif"/> <condition data="Scattered Thunderstorms"/> </forecast_conditions> </weather> </xml_api_reply> Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/ Share on other sites More sharing options...
dezkit Posted July 15, 2010 Share Posted July 15, 2010 Umm, you can use simpleXML instead, easier. Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1086230 Share on other sites More sharing options...
aeroswat Posted July 16, 2010 Author Share Posted July 16, 2010 Umm, you can use simpleXML instead, easier. I found this example on SimpleXML $library = simplexml_load_file('library.xml'); foreach ($library->shelf as $shelf) { printf("Shelf %s\n", $shelf['id']); foreach ($shelf->book as $book) { printf("Title: %s\n", $book->title); printf("Author: %s\n", $book->author); } } so can i place this string in the load file function or do I need to do something else to it before I use it in that function? http://www.google.com/ig/api?weather=New%20York Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087013 Share on other sites More sharing options...
aeroswat Posted July 16, 2010 Author Share Posted July 16, 2010 So I tried this... $forecast = simplexml_load_file('http://www.google.com/ig/api?weather=New%20York'); foreach ($forecast->forecast_conditions as $weather) { printf("Day of Week %s\n", $weather['day_of_week']); } And it didn't do anything. Any advice or direction would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087022 Share on other sites More sharing options...
dezkit Posted July 16, 2010 Share Posted July 16, 2010 try foreaching echo $weather->day_of_week["data"]; if doesnt work: echo $weather["day_of_week"]["data"]; Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087056 Share on other sites More sharing options...
aeroswat Posted July 16, 2010 Author Share Posted July 16, 2010 try foreaching echo $weather->day_of_week["data"]; if doesnt work: echo $weather["day_of_week"]["data"]; Tried both :/ Neither work Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087059 Share on other sites More sharing options...
dezkit Posted July 16, 2010 Share Posted July 16, 2010 <?php $forecast = simplexml_load_file('http://www.google.com/ig/api?weather=New%20York'); foreach ($forecast->weather->forecast_conditions as $weather) { echo $weather->day_of_week["data"]; } ?> lol Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087066 Share on other sites More sharing options...
aeroswat Posted July 16, 2010 Author Share Posted July 16, 2010 <?php $forecast = simplexml_load_file('http://www.google.com/ig/api?weather=New%20York'); foreach ($forecast->weather->forecast_conditions as $weather) { echo $weather->day_of_week["data"]; } ?> lol You are awesome! Thanks So I'm guessing anytime it has an encapsulating tag of its own it needs to be included in that foreach? Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087071 Share on other sites More sharing options...
dezkit Posted July 16, 2010 Share Posted July 16, 2010 <?php $forecast = simplexml_load_file('http://www.google.com/ig/api?weather=New%20York'); foreach ($forecast->weather->forecast_conditions as $weather) { echo $weather->day_of_week["data"]; } ?> lol You are awesome! Thanks So I'm guessing anytime it has an encapsulating tag of its own it needs to be included in that foreach? Yeah!! I thought you had included that thing lol, I thought it was a problem on php's side, but then I re read the xml. Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087072 Share on other sites More sharing options...
aeroswat Posted July 16, 2010 Author Share Posted July 16, 2010 <?php $forecast = simplexml_load_file('http://www.google.com/ig/api?weather=New%20York'); foreach ($forecast->weather->forecast_conditions as $weather) { echo $weather->day_of_week["data"]; } ?> lol You are awesome! Thanks So I'm guessing anytime it has an encapsulating tag of its own it needs to be included in that foreach? Yeah!! I thought you had included that thing lol, I thought it was a problem on php's side, but then I re read the xml. Ya i've just never done anything with xml before so I didn't know Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087077 Share on other sites More sharing options...
dezkit Posted July 16, 2010 Share Posted July 16, 2010 no problem Quote Link to comment https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.