# Parse XML with PHP Selective to show weather info

**URL:** <https://forum.kirupa.com/t/parse-xml-with-php-selective-to-show-weather-info/239560>\
**Category:** programming\
**Created:** [September 23, 2007, 1:04pm UTC](https://forum.kirupa.com/t/parse-xml-with-php-selective-to-show-weather-info/239560 "2007-09-23T13:04:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![therealvibe](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@therealvibe](https://forum.kirupa.com/u/therealvibe)\
**Post date:** [September 23, 2007, 1:04pm UTC](https://forum.kirupa.com/t/parse-xml-with-php-selective-to-show-weather-info/239560/1 "2007-09-23T13:04:55Z")

</div>

Dear All,

I am new at PHP and XML. I am developing a site which I want to display weather information. The only data I need is temperature. Low and High. And maybe a string to show a cloud or a sun. only 3 data. here is a dummy view of what I want to achieve.  
[http://arubaguide.org/arubaguide3/index.php?page=default](http://arubaguide.org/arubaguide3/index.php?page=default)

I used a parse XML to PHP from a tutorial here at kirupa:

\<?php  
// The XML file that you wish to be parsed  
$file = “[http://xml.weather.yahoo.com/forecastrss?p=AAXX0002&u=c](http://xml.weather.yahoo.com/forecastrss?p=AAXX0002&u=c)”;  
// This function tells the parser what to do with the data once it reaches the contents  
// that appear between tags.  
function contents($parser, $data){  
echo $data;  
}  
// This function tells the parser to place a \<b\> where it finds a start tag.  
function startTag($parser, $data){  
echo “\<b\>”;  
}  
// And this function tells the parser to replace the end tags with “\<b\>\<br /\>”  
function endTag($parser, $data){  
echo “\</b\>\<br /\>”;  
}  
// These lines create the parser and then set the functions for the parser to use when  
// reading the document.  
$xml\_parser = xml\_parser\_create();  
// Sets the functions for start and end tags  
xml\_set\_element\_handler($xml\_parser, “startTag”, “endTag”);  
// Sets the function for the contents/data  
xml\_set\_character\_data\_handler($xml\_parser, “contents”);  
// Opens the file for reading  
$fp = fopen($file, “r”);  
// Read the file and save its contents as the variable “data”  
$data = fread($fp, 80000);  
// This if statement does two things. 1) it parses the document according to our  
// functions created above. 2) If the parse fails for some reason it returns an  
// error message and also tells us which line the error occured at.  
if(!(xml\_parse($xml\_parser, $data, feof($fp)))){  
die("Error on line " . xml\_get\_current\_line\_number($xml\_parser));  
}  
// Free the memory used to create the parser  
xml\_parser\_free($xml\_parser);  
// Close the file when you’re done reading it  
fclose($fp);  
?\>

I used a yahoo weather feed for instance. But the result is a complete set of information I do not use:

[http://www.arubaguide.org/xml\_beginner.php](http://www.arubaguide.org/xml_beginner.php)

What PHP option should I use to select only the temperature and not to dislay the whole information? For instance to only select the low and high temperature?

I see this in the yahoo feed:

\<yweather:forecast day=“Sun” date=“23 Sep 2007” low=“27” high=“32” text=“Partly Cloudy” code=“30” /\>

So I could “select” the high and low and text partly cloudy to display a could picture?

Something like this??  
if (strcasecmp($name, “yweather:forecast[low]”) == 0) {  
echo “$data”;  
}

I am new to this. Please let me know if this could be done.

Regards,

Dwayne
