Forum Moderators: coopster

Message Too Old, No Replies

xml feed parsing

         

cheaperholidays

5:27 pm on Dec 13, 2010 (gmt 0)

10+ Year Member



Hi all

I need help with parsing an xml feed i can use simple pie but i could with help on the best way to display the data from here, say the top 5 offers description then link

Many thanks for looking

Mike

<?xml version="1.0" encoding="ISO-8859-15" ?>
- <WebDataFeed>
- <DataFeed>
<siteCode>BRIE</siteCode>
<siteName>Bridge Boatyard</siteName>
<productId>BH1432</productId>
<name>Sunstar</name>
<address>K & K Mouldings Ltd Bridge Road</address>
<place>Ely, Cambridgeshire</place>
<county />
<region>Cambridgeshire Waterways</region>
<postcode>CB7 4DY</postcode>
<country>UK</country>
<weeklyPrice>377 - 643</weeklyPrice>
<sleeps>4</sleeps>
<siteDescription>You start your holiday from Bridge Boatyard from the very heart of the peaceful and picturesque Fenland waterways &#150; the fleet is based in Ely, overlooked by its magnificent and world-famous cathedral. The varied choice of well-fitted and laid out boats sleep from 2 to 6 people, and the range includes traditional narrowboats as well as the sports cruiser style.</siteDescription>
<productDescription />
<clickThroughUrlProduct>http://www.hoseasons.co.uk/WebPages/EUBoat/ProductDetailPage.aspx?ISDLNK=1&SCODE=BRIE&ACODE=BH1432&TIDPDEUBOAT=boatInfo&PID=140&source=APIXMLFEED</clickThroughUrlProduct>
<clickThroughUrlSite>http://www.hoseasons.co.uk/WebPages/EUBoat/ProductDetailPage.aspx?ISDLNK=1&SCODE=BRIE&ACODE=BH1432&TIDPDEUBOAT=boatyardInfo&PID=140&source=APIXMLFEED</clickThroughUrlSite>
<imageUrl>http://www.hoseasons.co.uk/images/ProductImages/BoatsV3/BRIE-BH1432-EX1.jpg</imageUrl>
<partnerLink>http://www.hoseasons.co.uk/WebPages/EUBoat/ProductDetailPage.aspx?ISDLNK=1&SCODE=BRIE&ACODE=BH1432&bid=97&aid=cd179&pid=140&Source=APIXMLFEED&opt=</partnerLink>
<featureText><ul id='feature'><li>Recommended for two adults and two children </li><li>Centre cockpit design with folding canopy and windscreen </li><li>Warm air heating</li></ul></featureText>
<features>Bicycles Allowed</features>
<latitude>52.395432</latitude>
<longitude>0.25989</longitude>
</DataFeed>

coopster

3:48 pm on Dec 14, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you considered Simple XML [php.net]?

cheaperholidays

6:13 pm on Dec 14, 2010 (gmt 0)

10+ Year Member



Hi Coopster many thanks, this is the code we use with RSS feeds how would i amend this to work with simple xml>

<?php
require_once('phsimple');
$feed = new SimplePie('http://feeds.feedburner.com/virgin');
$feed->enable_cache(true);
$feed->set_cache_location($_SERVER['DOCUMENT_ROOT'] . '/cache');
$feed->set_cache_duration();
$feed->enable_order_by_date(false);
$feed->set_output_encoding('Windows-1252');
$feed->init();


// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code. The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.
?>



<?php
/*
Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
*/
foreach ($feed->get_items(4,0) as $item):
?>


<li> <a class="deals-direct" rel='nofollow' href="" target=_blank><?php echo $item->get_title(); ?></a></li>

<?php endforeach; ?>

coopster

9:12 pm on Dec 14, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I wouldn't amend it, but rewrite it using SimpleXML. There are some examples on the manual page that show you how to load the xml and then parse it.

cheaperholidays

3:02 pm on Dec 15, 2010 (gmt 0)

10+ Year Member



Hi Coopster this code wil output the xml in its entirety but in bold, which is great but not if you want to just grab some elements and not others in this xml file.

[cheaperholidays.com...]

These are the elements we want but i cannot figure out how to get this to work for the first 5 only.

<name>Sunstar</name> <siteDescription> <partnerLink>


<?php

$file = "cheaper.xml";

function contents($parser, $data){
echo $data;
}
function startTag($parser, $data){
echo "<b>";
}

function endTag($parser, $data){
echo "</b><br />";
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($file, "r");

$data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>

coopster

3:32 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's not SimpleXML, that's XML parser. Using SimpleXML and a combination of the first couple of examples on the SimpleXML Examples of Basic Usage will give you the desired data. You can set a counter to retrieve only the first five. The XML feed must be valid markup too. The link you provided shows 404.

cheaperholidays

4:08 pm on Dec 15, 2010 (gmt 0)

10+ Year Member



We are getting errors with simple xnl the module may be turned off on the server,,

Cannot instantiate non-existent class: simplexmlelement which is why i went the other way

coopster

4:50 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Wow. What version of PHP are you using on the server? If it is PHP >= 5.1.2 you should have it available:
[php.net...]

cheaperholidays

5:35 pm on Dec 15, 2010 (gmt 0)

10+ Year Member



PHP Version 4.3.10-16

coopster

6:15 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh, wow. PHP version 4 was discontinued [php.net] at the end of 2007. XML programming with PHP became 10 times easier with the release of PHP version 5. I realize you may not be in a position to upgrade, but you really, really need to upgrade your release version and I would upgrade to the latest level. Your XML programming effort alone is going to be worth the savings.