Forum Moderators: open

Message Too Old, No Replies

XML Parsed - How can I modify display?

         

breham

3:01 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



Hi,

I have been provided with a white label solution which provides data via XML.

I have no problem parsing the XML but the example code that the company provided displays the data in a very prescriptive way and I can't seem to modify it.

It is using PHP Switch/Case loops and therefore displays the data in the order it picks it up from the XML. i.e.

Image
review
price
release date

and so on. What I'd like to is take the XML, put it into variables and then display it as I want rather than just one bit after another.

The code is:

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);

// event handler for start elements in the XML
function startElement($parser, $name, $attrs)
{
global $lastTag;
// get the tag name
$lastTag = $name;
switch ($name) {
case "RETAILERS":
echo '';
break;
}
}

// event handler for end elements in the XML
function endElement($parser, $name)
{
// allow access to global variables
global $dvdTitle, $dvdLastRetailerID, $dvdPicURL, $dvdReview, $dvdReleaseDate, $dvdRetailer, $dvdTotalPrice, $dvdDeepLink, $dvdRetailerID;
// determine the name of the tag in the XML
switch ($name) {

case "IMAGEURL":
echo '<tr><td class="maintext"><img border=0 src="',$dvdPicURL,'"></td></tr>';
break;
case "TITLE":
echo '<tr><td class="maintext"><b>',$dvdTitle,'</b></td></tr>';
break;

case "RELEASED":
echo '<tr><td class="maintext">Release date : ',$dvdReleaseDate,'</td></tr>';
break;
case "REVIEW":
echo '<tr><td class="maintext">Review: ',substr($dvdReview,0,200),'...<br><br><a href="/dvd/fullreview.php?">Click Here For A Full Review</a></td></tr>';
break;
case "RETAILERS":
echo '';
break;
case "TOTAL":
echo '<table>';
// display those retailers with a price
if ($dvdTotalPrice!= '') {
echo '<tr>';
echo '<td class="shopprices" width="280">',$dvdRetailer,'</td>';
echo '<td class="shopprices" align="left" width="50">&pound;',$dvdTotalPrice,'</td>';
echo '<td class="shopprices" width="100" align="right"><a target=_blank href="redirect.php?retailer=',$dvdRetailerID,'&amp;deeplink=',urlencode($dvdDeepLink),'">Go to shop</a>';
echo '</tr></table>';
}
$dvdLastRetailerID = '';
$dvdTotalPrice = '';
$dvdRetailer = '';
$dvdDeepLink = '';
$dvdRetailerID = '';
break;
}
}

// event handler for the XML character data (i.e. the actual data in the XML)
function characterData($parser, $data)
{
global $lastTag, $dvdTitle, $dvdPicURL, $dvdReview, $dvdReleaseDate, $dvdRetailer, $dvdTotalPrice, $dvdDeepLink, $dvdRetailerID, $dvdLastRetailerID;
switch ($lastTag) {
case "TITLE":
$dvdTitle .= $data;
break;
case "IMAGEURL":
$dvdPicURL = $data;
break;
case "REVIEW":
$dvdReview .= $data;
break;
case "RELEASED":
$dvdReleaseDate = $data;
break;
case "NAME":
$dvdRetailer = $data;
break;
case "TOTAL":
$dvdTotalPrice = $data;
break;
case "PRODUCTURL":
if ($dvdRetailerID == $dvdLastRetailerID) {
$dvdDeepLink .= $data;
} else {
$dvdDeepLink = $data;
}
$dvdLastRetailerID = $dvdRetailerID;
break;
case "IDENT":
$dvdRetailerID = $data;
break;
}
}

Many thanks in advance for any help.
Brett