zink Posted March 7, 2007 Share Posted March 7, 2007 Hi there it's my first post here 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? Can someone show me where I am going wrong? 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 Link to comment https://forums.phpfreaks.com/topic/41582-xml-parse-help/ Share on other sites More sharing options...
btherl Posted March 7, 2007 Share Posted March 7, 2007 It may be to do with when you increment the counter. As for the multiple APRs, I think it's whitespace. You can try ignoring any data that looks like an empty string (this will cause trouble if you really do have a blank APR of course So better is to append all APRs together until you reach the end tag) The approach I've used is to have the data handler keep on appending until the end tag is reached, and have the end tag handler store the data. Link to comment https://forums.phpfreaks.com/topic/41582-xml-parse-help/#findComment-201512 Share on other sites More sharing options...
zink Posted March 7, 2007 Author Share Posted March 7, 2007 Hey btherl thanks for the quick reply. Any idea of how I do that? Sorry I am a proper n00b :'( Link to comment https://forums.phpfreaks.com/topic/41582-xml-parse-help/#findComment-201517 Share on other sites More sharing options...
zink Posted March 7, 2007 Author Share Posted March 7, 2007 anyone? Link to comment https://forums.phpfreaks.com/topic/41582-xml-parse-help/#findComment-201594 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.