Jump to content

Recommended Posts

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 :P

 

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>

Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087013
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087022
Share on other sites

	<?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?

Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087071
Share on other sites

	<?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.
Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087072
Share on other sites

	<?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!

Link to comment
https://forums.phpfreaks.com/topic/207771-xml-parsing/#findComment-1087077
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.