Help with filtering my XML feed
Posted: Mon Apr 27, 2009 12:15 pm
Hi, I am relatively new to php, and am in need of some basic xml filtering assistance.
I am storing some XML element values in an array, and I would like to be able to filter these before I output the html.
I would like to have a reusable method of filtering and outputting html where I can pass in a keyword and a corresponding xml element.
For example:
I am currently reading my xml document using the code below. Thanks for your help with this.
My xml looks like this.
I am storing some XML element values in an array, and I would like to be able to filter these before I output the html.
I would like to have a reusable method of filtering and outputting html where I can pass in a keyword and a corresponding xml element.
For example:
- Where the xml element "title" contains the string "t-shirt"
- or as a seperate query where the xml element "category" is equal to the string "t-shirt"
I am currently reading my xml document using the code below. Thanks for your help with this.
Code: Select all
<?php
class RSSFeed {
public $items = array();
public function addFeed($rss_url) {
$xml = simplexml_load_file($rss_url);
foreach($xml->channel->item as $Item) {
$this->Items[] = array( "title" => $Item->title,
"link" => $Item->link,
"description" => $Item->description,
"guid" => $Item->guid,
"price" => $Item->price,
"category" => $Item->category,
"image" => $Item->image,
"range" => $Item->range,
);
}
sort($this->Items);
}
public function exportAsHTML() {
foreach($this->Items as $Item) {
echo '<div>
<div><img src="'.$Item["image"].'"></div>
<div>£'.$Item["price"].'</div>
<div><a href="'.$Item["link"].'">'.$Item["title"].'</a></div>
</div>';
}
}
}
$rss = new RSSFeed();
$rss->addFeed("http://localhost/rss");
$rss->exportAsHTML();
?>Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Personalised Baby Gifts RSS Feed</title>
<description> BESPOKE T-SHIRT FOR NEW BABIES TO KEEP FOREVER WITH THEIR NAME AND BIRTH WEIGHT TO REMIND YOU HOW SMALL THEY WERE.</description>
<link></link>
<lastBuildDate>Thurs, 23 Apr 2009 11:12:55 -0100 </lastBuildDate>
<pubDate>Mon, 27 Apr 2009 18:10:10 +0100</pubDate>
<image url="/images/top.jpg" link=""></image>
<item>
<title>Sporty T</title>
<category>t-shirts</category>
<description>A personalised baby T-shirt made from soft 100% organic cotton in natural colour with shoulder poppers, they are available in two sizes.
SIZE 1 for small newborn babies up to 8lbs/4kg. SIZE 2 for big newborn babies up to 14lbs/7kg or babies 1-3 months.</description>
<link>/baby-t-shirts/index.php?type=sporty</link>
<guid>TC1</guid>
<range>sporty1</range>
<colour>green</colour>
<price>15.00</price>
<gender>boy</gender>
<image>/images/t_popup_sportA1.jpg</image>
</item>
<item>
<title>Sporty T</title>
<category>t-shirts</category>
<description>A personalised baby T-shirt made from soft 100% organic cotton in natural colour with shoulder poppers, they are available in two sizes.
SIZE 1 for small newborn babies up to 8lbs/4kg. SIZE 2 for big newborn babies up to 14lbs/7kg or babies 1-3 months.</description>
<link>/baby-t-shirts/index.php?type=sporty</link>
<price>15.00</price>
<guid>TD1</guid>
<range>sportyt</range>
<colour>red</colour>
<gender>girl</gender>
<image>/images/t_popup_sportA2.jpg</image>
</item>
</channel>
</rss>