Currently I am using this code to pull a RSS feed from Yahoo Weather but I would like to style it the way I want but I am not sure how.
<?php
$doc = new DOMDocument();
$doc->load('http://xml.weather.yahoo.com/forecastrss/10013_f.xml');
$channel = $doc->getElementsByTagName("channel");
foreach($channel as $chnl)
{
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten)
{
$describe = $itemgotten->getElementsByTagName("description");
$description = $describe->item(0)->nodeValue;
echo $description;
}
}
?>
There are 2 options:
Option 1
A different approach to the previous answer: If you look at the actual XML file, then you'll also see that the same description is also enclosed in individual tags:
<yweather:condition text="Fair" code="34" temp="84" date="Tue, 29 May 2012 6:49 pm EDT" />
<yweather:forecast day="Tue" date="29 May 2012" low="70" high="86" text="Partly Cloudy" code="30" />
<yweather:forecast day="Wed" date="30 May 2012" low="64" high="85" text="Scattered Thunderstorms" code="38" />
<yweather:forecast day="Thu" date="31 May 2012" low="56" high="75" text="Partly Cloudy" code="30" />
<yweather:forecast day="Fri" date="1 Jun 2012" low="63" high="69" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="2 Jun 2012" low="59" high="70" text="Showers" code="11" />
There's no reason why you can't query all this data seperately and wrap it all in whatever HTML tags you want
To get the attributes from, for example, the conditions
attributes, this works:
<?php
$doc = new DOMDocument();
$doc->load('http://xml.weather.yahoo.com/forecastrss/10013_f.xml');
$channel = $doc->getElementsByTagName("channel");
foreach($channel as $chnl){
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten) {
$nodes = $itemgotten->getElementsByTagName('condition')->item(0);
if ($nodes->hasAttributes()) {
foreach ($nodes->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo "Attribute '$name' :: '$value'<br />";
}
}
}
}
?>
Option 2
If you do choose the approach suggested in the previous answer, don't forget that you can only use the HTML Tags used within the description (unless you do all sorts of string manipulation within PHP), but you can still use / style those tags by using cascading styles in your CSS:
div.description { /*general div styles*/ }
div.description b { /*styles for anything within a b tag in the description div*/ }
...
...and so on
As you said @Michael, once you change the code like this:
$doc = new DOMDocument();
$doc->load('http://xml.weather.yahoo.com/forecastrss/10013_f.xml');
$channel = $doc->getElementsByTagName("channel");
foreach($channel as $chnl){
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten) {
$describe = $itemgotten->getElementsByTagName("description");
$description = $describe->item(0)->nodeValue;
echo "<div class='description'>" . htmlspecialchars($description) . "</description>";
}
}
You must create the necessary CSS to display the information as desired.