Re: To get an associative array from an XML-file?
| From: | Fredrik Johansson | Date: | Fri, 03 Jan 2003 13:25:19 +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-3168@lists.php.net to get a copy of this message | ||
Bertrand Mansion wrote:
le 2/01/03 23:19, Fredrik Johansson à Fredrik@phpportalen.net a écrit :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 intoEmre Bastuz wrote: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 ;)Hi Fredrik, Zitat von Fredrik Johansson <Fredrik@phpportalen.net>: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, EmreThis 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.
$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 ? I would prefer the first of these to exemples. Else, what happens to a XML-schema like this: <db name="databasename"> <name>Fredrik Johansson</name> </db> ? If you need some help with the XML-container I can see what I can do. I have an old parser that parses as the first example shows, it may be possible to convert that into a PEAR-Config interface. One more question, which is the top prio of DB and XML? An DB-container would be nice to ;) Regards, Fredrik Johansson