Searching in php books I found a simple solution to transform XML to ARRAY
Code:
<!-- articles.xml -->
<?xml version="1.0" ?>
<dev_articles>
<article id="310">
<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>Mitchell Harper</name>
<email>admin@reserva.com</email>
</author>
<summary>Interested in building your own CMS? In this article Mitchell shows us how to build...</summary>
<image>article_310.jpg</image>
<date_added>20030105011450</date_added>
</article>
<article id="309">
<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>Steve Knoblock</name>
<email>stevek@devarticles.com</email>
</author>
<summary>Interested in sending advanced email using PHP's mail() function... </summary>
<image>article_309.jpg</image>
<date_added>20030102184303</date_added>
</article>
</dev_articles>
PHP Code:
function fetch_xml($xml){
if(is_file($xml)){
$xml_data = file_get_contents($xml);
}
else{
$xml_data = $xml;
}
$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);
$root_tag = $assoc_arr[0]['tag'];
$base_tag = strtolower($assoc_arr[1]['tag']);
$i = 0;
foreach($assoc_arr as $key => $element){
if($element['tag'] != $root_tag){
if(!preg_match('/^\s+$/', $element['value'])){
$tag = strtolower($element['tag']);
$items[$i][$tag] = $element['value'];
if($tag == $base_tag){
$i++;
}
}
elseif(isset($element['attributes'])){
$items[$i]['id'] = $element['attributes']['ID'];
}
}
}
return $items;
}
$items = fetch_xml('articles.xml');
echo '<pre>';
print_r($items);
echo '</pre>';
PHP Code:
//Results
Array
(
[0] => Array
(
[id] => 310
[title] => Building a Multi-Page Article System With ASP/PHP
[url] => http://www.devarticles.com/art/1/310
[doc_type] => Tutorial
[name] => Mitchell Harper
[email] => admin@reserva.com
[author] =>
[summary] => Interested in building your own CMS? In this article Mitchell shows us how to build...
[image] => article_310.jpg
[date_added] => 20030105011450
[article] =>
)
[1] => Array
(
[id] => 309
[title] => Getting Intimate With PHP's Mail() Function
[url] => http://www.devarticles.com/art/1/309
[doc_type] => Tutorial
[name] => Steve Knoblock
[email] => stevek@devarticles.com
[author] =>
[summary] => Interested in sending advanced email using PHP's mail() function...
[image] => article_309.jpg
[date_added] => 20030102184303
[article] =>
)
)
But I still needing your help to improve this code ...
1) I would like to set keys based in the element level on the tree, for example in articles.xml author element has the child nodes name and email, thus the array keys should be [author_name] and [author_email] instead [name] and[email]. How to improve this in a undefined deep level???
2) How to improve parse to undefined number of attributes for undefined elements???
Bookmarks