Re: To get an associative array from an XML-file?
| From: | Bertrand Mansion | Date: | Fri, 03 Jan 2003 14:37:12 +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-3171@lists.php.net to get a copy of this message | ||
<Fredrik@phpportalen.net> wrote :
> Bertrand Mansion wrote:
>
>> le 2/01/03 23:19, Fredrik Johansson à Fredrik@phpportalen.net a écrit :
>>> 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 ?
>>
> 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>
Exact. I will use an 'attributes' array, then.
To do that in the Config class, I need to add a $attributes property to each
Config_container objects (which can actually be considered as nodes with
extra features). This will also be useful for Apache-like configurations
where you find sections like:
<directory /path/to/directory>
</directory>
In this case, '/path/to/directory' could be considered like the attribute.
I need to change the Apache parser consequently.
> 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.
I was thinking about using the XML_Tree parser but if you have something
better or faster, you can send it to me, I will see if I can integrate it.
> One more question, which is the top prio of DB and XML? An DB-container
> would be nice to ;)
DB is not my priority but I will do it anyway, just not now.
BTW, I am not sure that the Config class is very fast... I haven't done
benchmarks nor anything very difficult with it. I suppose it will work
better with ZE2 because there are a lot of references (there are references
to parent inside child, don't use var_dump with Config !)
Its purpose is mainly to create, modify Config files. Maybe I should add a
cache feature. For example, an XML config file could be cached to a PHP
array... But that's another topic.
I will post an example for using the latest Config from CVS to the list.
Regards,
Bertrand Mansion
Mamasam