hey..
Liked the look of this code, so gave it a go... been looking at the best way to convert xml to php for one of our projects, my indent is to make the array seem as if we've done a query or something..
i've modded the code for use with my own needs, but its still generic, it'll now do name_email like you wanted... and will also add a number to the field if it already exists (the annoying source i had did <address> <line>street</line> <line>county</line> </address>)
anyways, here's the stuff:
<!-- articles2.xml -->
<?xml version="1.0" ?>
<dev_articles>
<article id="310" status="active">
<title>Building a Multi-Page Article System With ASP/PHP</title>
<url>http://www.devarticles.com/art/1/310</url>
<doc_type>Tutorial</doc_type>
<author>
<name>
<first>Mitchell</first>
<last>Harper</last>
</name>
<email>admin@reserva.com</email>
</author>
<summary>Interested in building your own CMS? In this article Mitchell shows us how to build...</summary>
<images>
<image>arcticle_310.jpg</image>
</images>
<date_added>20030105011450</date_added>
</article>
<article id="309" status="inactive">
<title>Getting Intimate With PHP's Mail() Function</title>
<url>http://www.devarticles.com/art/1/309</url>
<doc_type>Tutorial</doc_type>
<author>
<name>
<first>Steve</first>
<last>Knoblock</last>
</name>
<email>stevek@devarticles.com</email>
</author>
<summary>Interested in sending advanced email using PHP's mail() function... </summary>
<images>
<image>arcticle_309a.jpg</image>
<image>arcticle_309b.jpg</image>
<image>arcticle_309c.jpg</image>
</images>
<date_added>20030102184303</date_added>
</article>
</dev_articles>
here's the function:
function fetch_xml($xml,$root_level){
// Read XML File
if(!($fp = fopen($xml, "r"))){
die("IO Error $xml_file");
}
while($chunk = fread($fp,4096)){
$xml_data .= $chunk;
}
// Do the PHP Crap
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
// Setup Some VARs
$root_tag = $assoc_arr[$root_level]['tag']; // Used to define starting node
$base_level = $root_level+1;
$base_tag = strtolower($assoc_arr[$base_level]['tag']); // Used to define base tag
$i = 0;
$prefix = "";
foreach($assoc_arr as $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^s+$/', $element['value'])){
$tag = strtolower($element['tag']);
if($element['type'] == 'open' && $element['level'] > ($base_level+1)){
// Add Prefix's for new Tags Under Base
$tag_prefix[$element['level']] = $tag;
} elseif($element['type'] == 'close' && $element['level'] > ($base_level+1)){
// Remove Prefix's when tags closed
$tag_prefix[$element['level']] = "";
} elseif($element['type'] == 'complete') {
// Generate Prefix
if(count($tag_prefix) > 0){
$str_prefix = "";
foreach($tag_prefix as $prefix){
if(!empty($prefix))
$str_prefix .= $prefix."_";
}
} // End Prefix
// Add Our Data to the Items Array
$extra = "";
while(isset($items[$i][$str_prefix.$tag.$extra])){
if(empty($extra))
$extra = 1;
$extra++;
}
$items[$i][$str_prefix.$tag.$extra] = $element['value'];
}
}
if($tag == $base_tag){
// Add Our Atttributes
if(isset($element['attributes']) && $element['type'] == 'open'){
$items[$i] = $element['attributes'];
}
// Start New Record
if($element['type'] == 'close'){
$i++;
}
}
}
}
return $items;
}
note the new parameter to start at different levels
and the output array:
Array
(
[0] => Array
(
[ID] => 310
[STATUS] => active
[title] => Building a Multi-Page Article System With ASP/PHP
[url] => http://www.devarticles.com/art/1/310
[doc_type] => Tutorial
[author_name_first] => Mitchell
[author_name_last] => Harper
[author_email] => admin@reserva.com
[summary] => Interested in building your own CMS? In this article Mitchell shows us how to build...
[images_image] => arcticle_310.jpg
[date_added] => 20030105011450
)
[1] => Array
(
[ID] => 309
[STATUS] => inactive
[title] => Getting Intimate With PHP's Mail() Function
[url] => http://www.devarticles.com/art/1/309
[doc_type] => Tutorial
[author_name_first] => Steve
[author_name_last] => Knoblock
[author_email] => stevek@devarticles.com
[summary] => Interested in sending advanced email using PHP's mail() function...
[images_image] => arcticle_309a.jpg
[images_image2] => arcticle_309b.jpg
[images_image3] => arcticle_309c.jpg
[date_added] => 20030102184303
)
)
hope this helps in some small way... got it working how i wanted =)
any more ideas?