Re: To get an associative array from an XML-file?

From: Date: Fri, 03 Jan 2003 10:05:09 +0000
Subject: Re: To get an associative array from an XML-file?
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3163@lists.php.net to get a copy of this message
le 2/01/03 23:19, Fredrik Johansson à Fredrik@phpportalen.net a écrit : > > > Emre Bastuz wrote: > >> Hi Fredrik, >> >> Zitat von Fredrik Johansson <Fredrik@phpportalen.net>: >> >> >>> This is probobly a real newbie question. But how do I get an associative >>> PHP-array from an XML-file? I have been looking at XML_Tree but it >>> confuses me. It doesen't have to be an array, but i'm going to read >>> config-values and need to have the config.xml in som kind of variable-form. >>> >>> >> some time ago I had to do the same. What I did was using the >> xml_parse_into_struct() function to have PHP create an array of the XML file. >> >> The real nasty work is restructuring this array into an associative >> array the way you need it, as you probably want to have something like >> >> <person> >> <firstname>Fredrik</firstname> >> <lastname>Johansson</lastname> >> <person> >> >> transformed into >> >> $foobar["firstname"] = "Fredrick", >> $foobar["lastname"] = "Johansson", >> etc. >> >> There are some examples for doing this restructuring on the PHP homepage >> (scroll >> down at the function docs for parse_into_struct()). >> >> I believe your question was kind of OT though ;-) >> >> Regards, >> >> Emre >> >> >> > I want to transform: > > <person> > <firstname>Fredrik</firstname> > <lastname>Johansson</lastname> > <person> > > To: > > $foobar['person']['firstname'] = 'Fredrik', > $foobar['person']['lastname'] = 'Johansson' > > And I can't se why it's OT, if none of the PEAR-classes can do this it > has to be implemented in one ;) At the moment, there are 2 versions of the Config class. I am currently working on the last one but it doesn't parse xml yet, it parses .ini, .conf, php arrays and apache configurations. XML is on the list. The only problem with xml to php array is how you handle attributes. For instance, how to translate: <db type="mysql" user="root" password="" /> <firstname>Fredrik</firstname> <lastname>Johansson</lastname> </db> Will this be translated into $foobar['db']['attributes']['type'] = 'mysql'; $foobar['db']['attributes']['user'] = 'root'; $foobar['db']['attributes']['password'] = ''; $foobar['db']['firstname'] = 'Fredrik'; $foobar['db']['lastname'] = 'Johansson'; or $foobar['db']['type'] = 'mysql'; $foobar['db']['user'] = 'root'; $foobar['db']['password'] = ''; $foobar['db']['firstname'] = 'Fredrik'; $foobar['db']['lastname'] = 'Johansson'; or are there any other solutions ? Apart from that, I think implementing such a parser for my Config class is very easy. For the moment, you can give it a .ini and it will convert it to an array by using the toArray() method. It will convert it to a string with the toSting() method. I have made a compatibility chart for conversion between different config types. I will post it soon. As the API has changed recently, examples in CVS are useless for now. I will post new ones soon too. This is still work in progress... Bertrand Mansion Mamasam

« previous php.pear.general (#3163) next »