#native_company# #native_desc#

Converting XML into a PHP data structure Page 3

By PHP Builder Staff
on December 25, 2002

Using An Array As A Data Structure

Knowing that the structure of the XML file is a tree, we need to find the best
way to represent that “tree” data in PHP. Well, my first idea
is to immediately consider a PHP array. Another option might be to build
objects similar to the DOM parser approach. I’ve decided not to write a
DOM parser, though (which you could easily do) because the DOM support is
coming along quickly enough. Why duplicate their efforts?
For simple XML, PHP arrays are perfect for the task because you can create
arrays of arrays of arrays and hence build a tree structure. Exactly
what we need for this learning exercise. Besides, there already exists a
plethora of functions built into the core PHP language for iterating
through arrays, pushing, popping, shifting, unshifting, splitting,
joining, slicing, etc.
To use the DOM model for inspiration, though, we’ll need to store several
pieces of information about a given XML tag. Each tag in XML will contain
4 pieces of information that we want to store:
  1. name of the tag,
  2. tag attributes (keys and values),
  3. data (the content inside the tag open and close),
  4. and possibly other nested tags.
A PHP array that can represent this simple XML tag (also refered to as a
node in the tree) might look as follows:


= array();

$node['_NAME']      = 'folder';     // stores the node (tag) name

$node['_DATA']      = 'content';    // stores the text content inside tags

$node['_ELEMENTS']  = array();      // stores sub-nodes in order

$node['key1']       = 'value1';     // stores all other node attributes

$node['key2']       = 'value2';     // stores all other node attributes

$node['key3']       = 'value3';     // stores all other node attributes


What I’ve done here is create an array of key and value pairs for all the
attributes in the node. Then, I’ve created 3 internal-use-only keys called
‘_NAME’, ‘_DATA’, ‘_ELEMENTS’ to store the tag name, tag data, and sub-node
array. By using the underscore (‘_’) I ensure that I’ll not conflict with
an attribute name. Using the sub-node array, we can now create arrays of
arrays of arrays and basically build our tree.
Using our XML example again, suppose you wanted to read in some information
from the file where name is ‘d.txt’… You’d first convert the XML into a
PHP array of arrays and then access the data with code like the following:



$owner      $data['drive'][0]['folder'][1]['file'][1]['owner'];

$comment    $data['drive'][0]['folder'][1]['file'][1]['_DATA'];