Click to See Complete Forum and Search --> : php 5- simple xml question
Okay when I use simple xml I use the following code. Being new at this I don't know how to manipulate this to get the data I want. Let’s say that the xml feed has four <description> sections. How can I change the code below to only get the <description> I want... (<description #2)?
<?
$xml = simplexml_load_file('http://www.quickweather.ca/cgi-bin/xml.cgi?key=6b8bfb1b0c5caea7c922f6cdea8acdb1&city=YYZ&tz=EST');
foreach ($xml->channel->item as $item){
echo $item->description."<br>";
echo $item->observations."<br>";
echo $item->temperature."<br>";
}
?>
rincewind456
01-01-2006, 04:43 PM
That code won't work with that xml page
this <?php
$xml = simplexml_load_file('http://www.quickweather.ca/cgi-bin/xml.cgi?key=6b8bfb1b0c5caea7c922f6cdea8acdb1&city=YYZ&tz=EST');
foreach ($xml->currentconditions as $item){
echo $item->cityname."<br>";
echo $item->observations."<br>";
echo $item->temperature."<br>";
}
?> will output
Toronto, ON
Fog
-0.2
so you should be able to work out that the linesecho $item->cityname"<br>"; equate to the lines<cityname>Toronto, ON</cityname> in the xml feed.
Thanks for that code, which helps me out a lot. :) I have another question. If I wanted to use the following link: (http://www.nhc.noaa.gov/index-at.xml) to get only the first <description>, and the third title...how would the code change to do that?
rincewind456
01-01-2006, 04:59 PM
If I've counted right :D
<?php
$xml = simplexml_load_file('http://www.nhc.noaa.gov/index-at.xml');
echo $xml->channel->title."<br>";
echo $xml->channel->item->title."<br>";
?>
How you learnt this stuff, you're really good at it...I know this may be a long shot, but is there a way that a php code could be built to get the data off of this webpage (http://twister.sbs.ohio-state.edu/text/canada/WWCN12.CWNT). There is no xml, but since its all text I was wondering if there is an easy php code to get the data?
rincewind456
01-01-2006, 05:17 PM
Like so$html = file_get_contents('http://twister.sbs.ohio-state.edu/text/canada/WWCN12.CWNT');
echo "<pre>".$html."</pre>";
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.