Do you know the structure of the xml and the tags which are of use to you? If so I've written a cheeky (i.e. some people might think crap) little couple of functions where you tell it what tags you want, what starts a record and it dumps it all to an array. This is what I did to test the amazon dev kit
/* Will include a list of tags I want that are scalars and a list that are arrays,
and what kicks off a new record */
// hit one of these, and you start a new product
$newRecordTag='Details';
//are outside the newRecordTag
$globalTags=array('TotalResults','TotalPages');
// can occur once within each newRecordTag
$scalarTags=array('AverageRating', 'SalesRank', 'ListPrice', 'OurPrice','ImageUrlSmall',
'ImageUrlMedium','ImageUrlLarge','ProductName','Asin', 'Manufacturer', 'Catalog');
// can occur many times within the newRecordTag
$multiTags=array('Author','Rating', 'Summary', 'Comment', 'Similarity', 'BrowseName');
// tags that occur in attributes - throw them into the pot
$attrTags=array('url');
$xmlRecords=array();//where they'll go
$currData=''; $currRecord=0;
$file='DevKit/XMLResponseSamples/webserv-example2-heavy.xml';
parse_xml_records( file_get_contents($file) );
echo "CHECK:".$xmlRecords[0]['Asin'].'<br>';
echo '<pre>'; print_r($xmlRecords); echo '<pre>';
function parse_xml_records($xmlContent)
{
// set up the XML parser and functions
$xml_parser = xml_parser_create();
// parser automatically puts tags into upper case, breaking xml standards - turn it off
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, 'cda_func');
if ( !xml_parse($xml_parser, $xmlContent) )
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
}
// when a new tag is found clear the data buffer
function startElement($parser, $name, $attrs)
{
global $currData, $attrTags, $xmlRecords, $currRecord;
$currData='';
if( $attrs )
{
foreach($attrs as $attrib => $value)
{
if( in_array($attrib, $attrTags) )
$xmlRecords[$currRecord][$attrib]=$value;
}
}
}
// puts the data buffer into the record with the tag name,
//or increments the record counter if $newRecordTag tag reached
function endElement($parser, $name)
{
global $currData, $currRecord, $xmlRecords;
global $newRecordTag, $scalarTags, $multiTags, $globalTags;
if($name==$newRecordTag)
$currRecord++;
if ( in_array($name, $scalarTags ) )
$xmlRecords[$currRecord][$name]=$currData;
elseif ( in_array($name, $multiTags) )
$xmlRecords[$currRecord][$name][]=$currData;
elseif( in_array($name, $globalTags) )
$xmlRecords[$name]=$currData;
}
// adds data to the buffer - may be called many times during the same tag.
function cda_func($parser, $data)
{
global $currData;
$currData.=$data;
}