I was using the following code to parse through the AWS XML feed. However, for some reason the character strings are represented multiple times with the subsequent times being null. This of course overwrites my previous settings so all the data is null. Here is the code:
/ Initialize the Row Count to One /
$row_count = 1;
/ Handles the Attributes for Opening Tags /
function startElement($parser, $name, $attrs='') {
global $current_tag;
$current_tag = $name;
switch($name) {
case 'DETAILS':
echo "<tr><td width=\"88%\" bgcolor=\"#FFFFFF\" height=\"67\"><font color=\"#53791A\">\n";
break;
case 'AUTHORS':
$authors = 0;
break;
default:
break;
}
}
/ Handles the Attributes for Closing Tags /
function endElement($parser, $name, $attrs='') {
global $current_tag;
switch($name) {
case 'DETAILS':
echo "<b>$ProductName</b></font><br>\n";
echo "$ReleaseDate<br>\n";
echo "ISBN: $ISBN</td>\n";
echo "<td bgcolor=\"#FFFFFF\" height=\"67\" align=\"center\">\n";
$current_size = getimagesize($ImageUrlSmall);
$current_img_width = $current_size[0];
$current_img_height = $current_size[1];
echo "<img alt=\"Picture of Book\" src=\"$ImageUrlSmall\" align=\"right\" border=\"0\"></a></td></tr></form>
break;
case 'AUTHORS':
$authors = 0;
break;
default:
break;
}
}
/ Read Character Data /
function characterData($parser, $data) {
global $current_tag;
// error checking line
echo "<P>$current_tag - $data</p>\n\n";
switch($current_tag) {
case 'TOTALRESULTS':
$TotalResults = $data;
break;
case 'PRODUCTNAME':
$ProductName = $data;
break;
case 'ASIN':
$ISBN = $data;
break;
case 'RELEASEDATE':
$ReleaseDate = $data;
break;
case 'IMAGEURLSMALL':
$ImageUrlSmall = $data;
break;
case 'AUTHOR':
if ($authors == 0) {
$Author = $data;
$authors = 1;
} else {
$Author = $Author . ", " . $data; }
break;
default:
break;
}
}
/ Declare the Character Set - UTF-8 is the Default /
$type = 'UTF-8';
/ Create the Parser /
$xml_parser = xml_parser_create($type);
// set some parser options
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, $type);
// this tells PHP what functions to call when it finds an element
// these funcitons also handle the element's attributes
xml_set_element_handler($xml_parser, 'startElement','endElement');
// this tells PHP what function to use on the character data
xml_set_character_data_handler($xml_parser, 'characterData');
if (!($fp = fopen($xmlconnectURL, 'r'))) {
die("Could not open $xml_file for parsing!\n");
}
// loop through the file and parse baby!
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo 'ERROR'."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
When I leave that error checking line I get this:
ISBN: 98723496123
ISBN:
ISBN:
This will happen for each piece of character data and the number of times varies. As you can see, the latest null one sets my data to null so nothing gets displayed. Any ideas on what I'm doing wrong and how to correct it?