Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML to ARRAY

Status
Not open for further replies.

marcoBR

Programmer
Apr 20, 2001
7
BR
Searching in php books I found a simple solution to transform XML to ARRAY

Code:
<!-- articles.xml -->
<?xml version=&quot;1.0&quot; ?>
<dev_articles>
	<article id=&quot;310&quot;>
		<title>Building a Multi-Page Article System With ASP/PHP</title>
		<url>[url]http://www.devarticles.com/art/1/310[/url]</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=&quot;309&quot;>
		<title>Getting Intimate With PHP's Mail() Function</title>
		<url>[url]http://www.devarticles.com/art/1/309[/url]</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:
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:
//Results
Array
(
    [0] => Array
        (
            [id] => 310
            [title] => Building a Multi-Page Article System With ASP/PHP
            [url] => [URL unfurl="true"]http://www.devarticles.com/art/1/310[/URL]
            [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] => [URL unfurl="true"]http://www.devarticles.com/art/1/309[/URL]
            [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 ... :rolleyes:

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. How to improve this in a undefined deep level???

2) How to improve parse to undefined number of attributes for undefined elements???

Regards!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top