PHP Classes

instead of index value why not associative value for $structure

Recommend this page to a friend!

      Generic XML parser class  >  All threads  >  instead of index value why not...  >  (Un) Subscribe thread alerts  
Subject:instead of index value why not...
Summary:change of array values
Messages:2
Author:eli orellana
Date:2012-01-16 09:49:52
Update:2012-01-22 02:45:59
 

  1. instead of index value why not...   Reply   Report abuse  
Picture of eli orellana eli orellana - 2012-01-16 09:49:53
I have been playing around with this Class and was wondering why you chose the specific array key value pair arrangement that you did.

I can understand that your method has arranged the array to have as much information as possible and accessible if you knew the information. But for simpler xml files a simple structure like this would be perfect

$xmlArr = array('root' => array(
'child' => array(
'name' =>'some name',
'value' => 'some value'
)
);

that way it can be accessed using

echo $xmlArr['root'][0]['name'];
echo $xmlArr['root'][0]['value'];

My XML document has a simple structure like this, and is why I was wondering why so much complexity with the array $parser->structure

<settings>
<title>Database Settings</title>
<setting>
<name>Host</name>
<value>localhost</value>
</setting>
</settings>

which would ideally be accessed with

echo $xmlArr['settings']['title'];
echo $xmlArr['settings'][0]['name'];
echo $xmlArr['settings'][0]['value'];

Also, out of curiosity. Do you have benchmarks on the benefits of caching the XML files rather than keep reading and extrating the data?

Thank you for contributing this Class, it has been extremely helpful and also further helped me learn PHP. I made some changes to the formatting and to the functions to tweak the output to my needs.

As a request can you update the example page that executes the Dump functions to also show methods used to access the array data from example.xml through $parser->structure

  2. Re: instead of index value why not...   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-01-22 02:45:59 - In reply to message 1 from eli orellana
Well, this approach was chosen because you can have a single index for all XML elements. There is no need for nested array structures.

You can obtain the index of an element by joining the path of the parent element a comma and the number of the child element among all elements of the parent. So the index of the root element is always "0" and the first child element is "0,0" and so on.

As for caching, I did not do any benchmarks but it is a matter of common sense to expect that parsing XML files is always slower than loading a cache file and unserialize it.