PHP and XML Parsing trouble

I am working to create an online library for a non-profit org that focuses on child care. They want a library to catelouge their inventory of childrens toys and games. To do this I am using PHP and XML, XML to store the information and php to parse and display it.

I have followed the example @ http://www.kirupa.com/web/xml_php_parse_intermediate.htm in order to create the parse function and it works… kinda… the issue I am running across is that instead of parsing all the elements at once It seems to want to do them two at a time. Examples below.

What I expect to get is one div element like the one below.


<div class=item>
		<span class=name>Item Name</span>
		<span class=category>Category</span>
		<span class=description>Item Description</span>
		<img src="image source location"/>
		<span class=location>Location information</span>
</div>

what I actually get is


    <div class=item>
		<span class=name>Animal Lotto</span>
		<span class=category>Game</span>
		<span class=description></span>
		<img src=/>
		<span class=location></span>
	</div>
	 <div class=item>
		<span class=name></span>
		<span class=category></span>
		<span class=description>Children love the excitement of playing lotto, and you'll love their enthusiasm for learning as they match colorful picture cards on the lotto board.</span>
		<img src=/>
		<span class=location></span>
	</div>
	<div class=item>
		<span class=name></span>
		<span class=category></span>
		<span class=description></span>
		<img src="images/Animal Lotto.png"/>
		<span class=location></span>
	</div>
	<div class=item>
		<span class=name></span>
		<span class=category></span>
		<span class=description></span>
		<img src=/>
		<span class=location>Region 1, Region 3</span>
	</div>

My XML file is laid out like so:


<?xml version="1.0" encoding="ISO-8859-1"?>
<inventory>
<item>
	<itemname>Animal Lotto</itemname>
	<itemcategory>Game</itemcategory>
	<itemdescription>Children love the excitement of playing lotto, and you'll love their enthusiasm for learning as they match colorful picture cards on the lotto board.</itemdescription>
	<itemimage>"images/Animal Lotto.png"</itemimage>
	<itemlocation>Region 1, Region 3</itemlocation>
</item>
</inventory>

and the php file that parses / outputs it is like so:


<?php 

$xml_file = "library.xml"; 

$xml_name_key = "*INVENTORY*ITEM*ITEMNAME"; 
$xml_category_key = "*INVENTORY*ITEM*ITEMCATEGORY"; 
$xml_description_key = "*INVENTORY*ITEM*ITEMDESCRIPTION";
$xml_image_key = "*INVENTORY*ITEM*ITEMIMAGE";
$xml_location_key = "*INVENTORY*ITEM*ITEMLOCATION"; 

$item_array = array(); 

$counter = 0; 
class xml_item{ 
    var $name, $category, $description, $image, $location; 
} 

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_name_key, $xml_category_key, $xml_description_key, $xml_image_key, $xml_location_key, $counter, $item_array; 
    switch($current_tag){ 
       case $xml_name_key: 
            $item_array[$counter] = new xml_item(); 
            $item_array[$counter]->name = $data; 
            break; 
       case $xml_category_key: 
            $item_array[$counter]->category = $data; 
            $counter++; 
            break; 
       case $xml_description_key: 
            $item_array[$counter]->description = $data; 
            $counter++; 
            break; 
	   case $xml_image_key: 
            $item_array[$counter]->image = $data; 
            $counter++; 
            break; 
	   case $xml_location_key: 
            $item_array[$counter]->location = $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>testing</title> 
<link href="basic.css" rel="stylesheet" type="text/css">
</head> 
<body bgcolor="#FFFFFF"> 
<?php 
for($x=0;$x<count($item_array);$x++){ 
echo "	<div class=item>
		<span class=name>" . $item_array[$x]->name . "</span>
"; 
echo "		<span class=category>" . $item_array[$x]->category . "</span>
"; 
echo "		<span class=description>" . $item_array[$x]->description . "</span>
"; 
echo "		<img src=" . $item_array[$x]->image . "/>
"; 
echo "		<span class=location>" . $item_array[$x]->location . "</span>
	</div>
";
} 
?>
</div>
</body> 
</html> 

If anyone can help me figure out why it’s splitting it into 4 divs instead of one I will be very greatful.