 |
|
|
  |
|
Self taught incompetent
|
|
Join Date: Nov 2004
Location: North East UK
Posts: 122
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XML Parsing with PHP Help
Hi all,
I'm trying to get my music price comparison site off the ground and I'm having a few probs with parsing the XML feed from medifusion.
I've already got my dvd and games sites running but this one is a bit trickier because it has nested tags in the XMl and i'm no expert so im having trouble getting the data out of those tags, specifically Artist and Track info.
The xml feed is formatted as below..
<cds>
-----<cd>
----------<id></id>
----------<title></title>
----------<artists>
--------------<artist></artist>
----------</artists>
----------<released></released>
----------<review></review>
----------<rrp></rrp>
----------<tracks>
--------------<track></track>
--------------<track></track>
--------------<track></track>
--------------<track></track>
--------------<track></track>
----------</tracks>
----------<publisher></publisher>
----------<imageURL></imageURL>
----------<thumbnailURL></thumbnailURL>
----------<pricesLastCached></pricesLastCached>
----------<minimumPrice></minimumPrice>
----</cd>
</cds>
I can get all the info in the tags under "cd", but other ones such as "artist" and "track" - I dont know how to do it.
any help appreciated as ever.
|
|
|
|
  |
|
Registered User
|
|
Join Date: Dec 2004
Posts: 26
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I'm not expert on the feeds as of yet, I'm learning bit by bit. But I'm breaking the sample code to do something particular and to be unique.
If you look at fetchprice.php script and look how the parsing is done for each retailler, it should give you an idea how to cope with the track listings.
If you are still having problems by the time I get round to doing the CDs I will be happy to help. But don't expect it to be too soon.
Jason
__________________
No matter how good I am. I',m still going to Die.
falklands.info
|
|
|
|
  |
|
Driving to win
|
|
Join Date: Aug 2003
Location: If I'm not at home, I'm in hospital
Posts: 7,363
|
Thanks: 5
Thanked 8 Times in 5 Posts
|
|
Can you post the code that you're using to parse the xml at the moment then I can probably work out for you what you need to add in to process the artists and tracks
__________________
Never argue with idiots. They just drag you down to their level and then beat you with their experience.
If ignorance is bliss then some of the people I know must be orgasmic.
|
|
|
|
  |
|
Self taught incompetent
|
|
Join Date: Nov 2004
Location: North East UK
Posts: 122
|
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hope this comes out ok. let me know if not and i'll email it to you. this is just a test page i set up. it wont return any data as you need the includes.php file. maybe you can figure it out without needing to run it. i'm sure its easy for a php expert. at the moment i havent added anything to display the artist - beacuse it wasnt displaying anyway when i did add it
<html>
<head>
<p align="center">PHP - XML feed test page</p>
</head>
<body>
<?php
include('includes.php');
// get the search string passed to the page, and the page number if there is one
$searchString = '';
if (empty($_POST)) {
$searchString = $_GET['title'];
} else {
$searchString = $_POST['title'];
}
$thisPage = 1;
if (!empty($_GET)) {
$thisPage = $_GET['page'];
}
$searchString = urlencode($searchString);
// this is the URL that needs to be requested to get the XML results
$serviceURL = 'http://www.find-services.co.uk/cd/cdSearch.aspx?title=';
$serviceURL .= $searchString;
$serviceURL .= '&page=';
$serviceURL .= $thisPage;
$serviceURL .= '&site=';
$serviceURL .= getSiteToken();
// call the service, the returned XML is in $Page
$Page = GetPage($serviceURL,'');
// echo "<br>the data returned was ", $Page;
// declare some variables for use in parsing the XML
$cdID = '';
$cdPicURL = '';
$cdTitle = '';
$moreResults = '';
$thisPage = '';
// create an XML parser
$parser = xml_parser_create();
// top of the results table
echo '<table width="100%">';
// set options and event handlers for the XML parser
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");
// parse the XML and stop if there is a problem
if (!xml_parse($parser, $Page)) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// free up memory from this XML parser
xml_parser_free($parser);
// end the results table
echo '</table>';
echo '<div>';
//previous page link
if (!$page) {$page=1; $thisPage=1;}
// if there are more results, then display a link to the next page of results, i.e. call the same PHP page again
if ($moreResults == '1') {
echo '<a href="searchTitles.php?title=',$searchString,'& ;page=',$thisPage+1,'" class="olink">Next page of results</a><br /><br />';
}
// if we are on a page greater than 1, then display a link to previous page of results
if ($page>1 OR $thisPage>1 OR $moreResults)
if ($thisPage>1 OR $page>1)
echo '<a href="searchTitles.php?title=',$searchString,'& ;page=',$thisPage-1,'" class="olink">Previous page of results<br /><br /></a>';
echo '</div>';
// event handler for start elements in the XML
function startElement($parser, $name, $attrs)
{
global $lastTag, $released, $review, $rrp, $publisher, $imageURL, $thumbnailURL, $priceslastCached, $minprice, $artist;
// get the tag name
$lastTag = $name;
switch ($lastTag) {
case "CD":
$CDid = '';
$title = '';
$released = '';
$review = '';
$rrp = '';
$publisher = '';
$imageURL = '';
$thumbnailURL = '';
$pricesLastCached = '';
$minprice = '';
$artist = '';
break;
case "ARTISTS":
$artist= '';
break;
}
}
// event handler for end elements in the XML
function endElement($parser, $name)
{
global $lastTag, $Cntr, $columns, $CDid, $title, $released, $review, $rrp, $publisher, $imageURL, $thumbnailURL, $priceslastCached, $minprice, $artist;
switch ($name) {
case "CD":
if (!$imageURL=='') {
echo '<a href="fetchPrices.php?cd=',$CDid,'" class="title-name"><img src="',$imageURL,'" style="float:left;border:none;margin-left:16px;margin-right:10px;margin-bottom:0px;margin-bottom:8px;">',$title,'</a>
<br /><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"rrp: ",$rrp,'</a><br>
<a href="fetchPrices.php?cd=',$CDid,'" class="olink">Compare prices</a><br><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"cheapest dvd price: £",$minprice,'</a><br>',substr($review,0,200),'';
} else {
echo '<a href="fetchPrices.php?cd=',$CDid,'" class="title-name"><img src="images/blank-dvd.gif" style="float:left;border:none;margin-left:16px;margin-right:10px;margin-bottom:0px;margin-bottom:8px;">',$title,'</a>
<br /><a href="fetchPrices.php?cd=',$CDid,'" class="olink">Compare prices</a><br><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"cheapest dvd price: £",$minprice,'</a><br>',substr($review,0,200),'';
}
$cntr = $cntr + 1;
if ($cntr >= $columns) {
$cntr = 1;
echo '<tr valign="middle"><div style="width:100%;margin-top:0px;margin-bottom:15px;margin-top:0px;border-bottom:solid 1px #A70016;"> </div></tr>';
}
$title = '';
break;
}
}
// event handler for the XML character data (i.e. the actual data in the XML)
function characterData($parser, $data)
{
global $lastTag, $imageURL, $title, $review, $CDid, $released, $rrp, $publisher, $thumbnailURL, $priceslastCached, $minprice;
switch ($lastTag) {
case "ID":
$CDid = $data;
break;
case "TITLE":
$title .= $data;
break;
case "RELEASED":
$released = $data;
break;
case "REVIEW":
$review .= $data;
break;
case "RRP":
$rrp = $data;
break;
case "PUBLISHER":
$publisher = $data;
break;
case "THUMBNAILURL":
$imageURL = $data;
break;
case "PRICESLASTCACHED":
$pricesLastCached = $data;
break;
case "MINIMUMPRICE":
$minprice = $data;
break;
case "ARTIST":
$artist = $data;
break;
}
}
?>
</body>
</html>
|
|
|
|
  |
|
Driving to win
|
|
Join Date: Aug 2003
Location: If I'm not at home, I'm in hospital
Posts: 7,363
|
Thanks: 5
Thanked 8 Times in 5 Posts
|
|
OK - this may not be perfect as I haven't tested it but I think if you change your code to:
Code:
<html>
<head>
<p align="center">PHP - XML feed test page</p>
</head>
<body>
<?php
include('includes.php');
// get the search string passed to the page, and the page number if there is one
$searchString = '';
if (empty($_POST)) {
$searchString = $_GET['title'];
} else {
$searchString = $_POST['title'];
}
$thisPage = 1;
if (!empty($_GET)) {
$thisPage = $_GET['page'];
}
$searchString = urlencode($searchString);
// this is the URL that needs to be requested to get the XML results
$serviceURL = 'http://www.find-services.co.uk/cd/cdSearch.aspx?title=';
$serviceURL .= $searchString;
$serviceURL .= '&page=';
$serviceURL .= $thisPage;
$serviceURL .= '&site=';
$serviceURL .= getSiteToken();
// call the service, the returned XML is in $Page
$Page = GetPage($serviceURL,'');
// echo "<br>the data returned was ", $Page;
// declare some variables for use in parsing the XML
$cdID = '';
$cdPicURL = '';
$cdTitle = '';
$moreResults = '';
$thisPage = '';
// create an XML parser
$parser = xml_parser_create();
// top of the results table
echo '<table width="100%">';
// set options and event handlers for the XML parser
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($parser, "startElement", "endElement");
xml_set_character_data_handler($parser, "characterData");
// parse the XML and stop if there is a problem
if (!xml_parse($parser, $Page)) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// free up memory from this XML parser
xml_parser_free($parser);
// end the results table
echo '</table>';
echo '<div>';
//previous page link
if (!$page) {$page=1; $thisPage=1;}
// if there are more results, then display a link to the next page of results, i.e. call the same PHP page again
if ($moreResults == '1') {
echo '<a href="searchTitles.php?title=',$searchString,'&page=',$thisPage+1,'" class="olink">Next page of results</a><br /><br />';
}
// if we are on a page greater than 1, then display a link to previous page of results
if ($page>1 OR $thisPage>1 OR $moreResults)
if ($thisPage>1 OR $page>1)
echo '<a href="searchTitles.php?title=',$searchString,'&page=',$thisPage-1,'" class="olink">Previous page of results<br /><br /></a>';
echo '</div>';
// event handler for start elements in the XML
function startElement($parser, $name, $attrs)
{
global $lastTag, $released, $review, $rrp, $publisher, $imageURL, $thumbnailURL, $priceslastCached, $minprice, $artist;
// get the tag name
$lastTag = $name;
switch ($lastTag) {
case "CD":
$CDid = '';
$title = '';
$released = '';
$review = '';
$rrp = '';
$publisher = '';
$imageURL = '';
$thumbnailURL = '';
$pricesLastCached = '';
$minprice = '';
$artist = '';
break;
}
}
// event handler for end elements in the XML
function endElement($parser, $name)
{
global $lastTag, $Cntr, $columns, $CDid, $title, $released, $review, $rrp, $publisher, $imageURL, $thumbnailURL, $priceslastCached, $minprice, $artist;
switch ($name) {
case "CD":
if (!$imageURL=='') {
echo '<a href="fetchPrices.php?cd=',$CDid,'" class="title-name"><img src="',$imageURL,'" style="float:left;border:none;margin-left:16px;margin-right:10px;margin-bottom:0px;margin-bottom:8px;">',$title,'</a>
<br /><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"rrp: ",$rrp,'</a><br>
<a href="fetchPrices.php?cd=',$CDid,'" class="olink">Compare prices</a><br><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"cheapest dvd price: £",$minprice,'</a><br>',substr($review,0,200),'';
} else {
echo '<a href="fetchPrices.php?cd=',$CDid,'" class="title-name"><img src="images/blank-dvd.gif" style="float:left;border:none;margin-left:16px;margin-right:10px;margin-bottom:0px;margin-bottom:8px;">',$title,'</a>
<br /><a href="fetchPrices.php?cd=',$CDid,'" class="olink">Compare prices</a><br><a href="fetchPrices.php?cd=',$CDid,'" class="subhead">',"cheapest dvd price: £",$minprice,'</a><br>',substr($review,0,200),'';
}
$cntr = $cntr + 1;
if ($cntr >= $columns) {
$cntr = 1;
echo '<tr valign="middle"><div style="width:100%;margin-top:0px;margin-bottom:15px;margin-top:0px;border-bottom:solid 1px #A70016;"> </div></tr>';
}
$title = '';
break;
}
}
// event handler for the XML character data (i.e. the actual data in the XML)
function characterData($parser, $data)
{
global $lastTag, $imageURL, $title, $review, $CDid, $released, $rrp, $publisher, $thumbnailURL, $priceslastCached, $minprice;
switch ($lastTag) {
case "ID":
$CDid = $data;
break;
case "TITLE":
$title .= $data;
break;
case "RELEASED":
$released = $data;
break;
case "REVIEW":
$review .= $data;
break;
case "RRP":
$rrp = $data;
break;
case "PUBLISHER":
$publisher = $data;
break;
case "THUMBNAILURL":
$imageURL = $data;
break;
case "PRICESLASTCACHED":
$pricesLastCached = $data;
break;
case "MINIMUMPRICE":
$minprice = $data;
break;
case "ARTIST":
if ($artist<> ''){
$artist = $artist . ", " . $data;
}
if ($artist == ''){
$artist = $data;
}
break;
}
}
?>
</body>
</html>
and then echo $artist wherever you want the artist list to appear it should work - if it still does not work then if you can email both files to me I'll have a play tonight for you
__________________
Never argue with idiots. They just drag you down to their level and then beat you with their experience.
If ignorance is bliss then some of the people I know must be orgasmic.
|
|
 |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
bit of a pickle, php xml parser
|
morleymouse |
Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing |
5 |
20-01-06 02:52 PM |
|
Anyone anygood with xml, php, mysql and Affiliate Window
|
AnnonnyMouse |
The Affiliate Marketing Lounge |
1 |
23-09-04 11:00 AM |
|
xml and php route on feed, page switch problem
|
ryanbof |
TradeDoubler |
1 |
23-08-04 02:20 AM |
|
Transforming TD XML feeds with a stylesheet in PHP
|
Andy |
TradeDoubler |
0 |
12-08-04 03:32 PM |
|
PHP XML Parser
|
morleymouse |
Widgets, Coding, AJAX, PHP - Technology & Affiliate Marketing |
3 |
15-12-03 11:06 PM |
|