Jump to content

XML parse help


zink

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.