General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 10th, 2003, 01:40 PM
marcoBR marcoBR is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 4 marcoBR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
XML to ARRAY

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>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="309">
		<title>Getting Intimate With PHP's Mail() Function</title>
		<url>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 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($parserXML_OPTION_CASE_FOLDING0);
    
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
    
$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 CMSIn 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???

Regards!!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > XML to ARRAY


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT