Converting XML Into a PHP Data Structure - Using an Array as a Data Structure (Page 3 of 9 )
Knowing that 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:
name of the tag,
tag attributes (keys and values),
data (the content inside the tag open and close),
and possibly other nested tags.
A PHP array that can represent this simple XML tag (also referred to as a node in the tree) might look as follows: $node = 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: $file_name = $data['drive']['folder']['file']['name']; $owner = $data['drive']['folder']['file']['owner']; $comment = $data['drive']['folder']['file']['_DATA'];