I'm reading in XML from a file. The file has NO carriage returns in it.
I have set up my php script to print out certain tags along with their attributes and content. For debugging purposes, I have included a default in my switch that prints out any tags that were not printed previously, but with a special note next to them.
What is happening is that aproximately half of my XML is not being displayed. When I say half, I mean that the parent tag is not being displayed, but it's children are.
Here is an example of my XML: (it's not very pretty :-)
<block editable="true" display="show" type="oneimg" number="1"><image align="center" height="144" width="329"><source>http://images.followers.net/sites/SITES0000008/SITES0000008_35.gif</source><alt>Query: SELECT image_description as alt, image_width as width, image_height as height, image_display_path as path FROM imagelibrary WHERE image_name='SITES0000008_35.gif' </alt></image></block>
I'll post the code I'm using below, but the block tags are ignored completely, as are the image tags. However the src and alt tags show fine.
Anyone have any suggestions?
<code>
$currentTag = "";
$attributes = "";
function startElement($parser, $name, $attrs) {
global $currentTag, $attributes;
$currentTag = $name;
$attributes = $attrs;
}
function endElement($parser, $name) {
global $currentTag, $attributes;
$currentTag = "";
$attributes = "";
}
// process data between tags
function characterData($parser, $data) {
global $currentTag, $attributes;
// format the data
switch ($currentTag) {
case "title":
echo "<b>Title:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
case "description":
echo "<b>Description:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
case "keywords":
echo "<b>Keywords:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
case "creation_date":
echo "<b>Creation_date:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
case "block":
echo "<b>Block:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
default:
echo "<b><i>default:</i>$currentTag:</b> $data <b>Attributes:</b> ";
array_walk($attributes, 'array_print');
echo "<br />\n";
break;
}
}
function array_print ($item, $key) {
echo "<i>$key</i>=$item, ";
}
// initialize parser
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
// read and parse data
$fp = fopen($PagePath, 'r');
while ($data = fread($fp, filesize ($PagePath)))
{
// error handler
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}else{
echo "The Parser Just Ran, (Quick! Catch It!)<br />";
}
}
// clean up
xml_parser_free($xml_parser);
</code>
P.S. Most of this code copied from an article on devshed.com