Hi there
I have a php script that is parsing my XML document but it is doing something crazy with the tags - like displaying "APR" 4 times for each record when there is only 1 entry for it!
I have worked with CSVs before but this is the first time with XML.
URL
http://www.moneygum.com/credit-card.php
XML
http://www.moneygum.com/xml/creditcard.xml
PHP Code
<?php
$xml_file = "xml/creditcard.xml";
$xml_cardname_key = "*ROOT*MEDIA*ROW*SUPPLIERDISPLAYNAME";
$xml_cardapr_key = "*ROOT*MEDIA*ROW*CARDSTANDARDAPRPURCHASES";
$xml_cardimg_key = "*ROOT*MEDIA*ROW*IMAGEURL";
$story_array = array();
$counter = 0;
class xml_story{
var $cardname, $cardapr, $cardimg;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_cardname_key, $xml_cardapr_key, $xml_cardimg_key, $counter, $story_array;
switch($current_tag){
case $xml_cardname_key:
echo"current tag = ".$current_tag."<br>";
$story_array[$counter] = new xml_story();
$story_array[$counter]->cardname = $data;
break;
case $xml_cardapr_key:
echo"current tag = ".$current_tag."<br>";
$story_array[$counter]->cardapr = $data;
$counter++;
break;
case $xml_cardimg_key:
echo"current tag = ".$current_tag."<br>";
$story_array[$counter]->cardimg = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
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);
?>
<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<table width="450">
<tr>
<td>
<?php
for($x=0;$x<count($story_array);$x++){
echo "\t<h2>" . $story_array[$x]->cardname . "</h2>\n";
echo "\t\t\n";
echo "\t<b>APR:</b><i>" . $story_array[$x]->cardapr . "</i>\n";
}
?>
</td>
</tr>
</table>
Array is:<br>
<?php
print_r($story_array);
?>
</body>
</html>
Can someone show me where I am going wrong? Or another way of doing this it would be great!!
p.s. I am almost certain it's something to do with whitespace (line break I think) within the tags but not sure how to get around this?
Thanks a lot
Zink