I’m working on a project where I needed to convert an XML doc into an associative array in PHP easily. I hadn’t done any work with XML in PHP yet, so I started digging around the usual places.
Soon, it looked like my best option would be to create a completely-custom XML parsing engine out of PHP’s built-in XML Parser. I started working on a set of code based off of the External Entity Example since that is the only example that actually incorporates the ability to retrieve data and not just attributes. My results were slow and very error prone. Why is this so tough? XML isn’t exactly new, and it is very widespread. Similarly, PHP is an extremely popular language. So, why is it so hard to combine the two and work with them easily? I shouldn’t have to create a custom syntax parser just to be able to read a standard format document into a PHP data structure.
I started to look around again. That’s when I found PEAR’s XML_Parser. Great! This looks to be just what I want. It deals with all the junk, and tries its best to just let me traverse the document. I started using the examples and quickly realized something, XML_Parser may work great for a document definition that never changes, but I want something that isn’t rigidly tied down to a very legalistic expectation of data. Is it really so much to ask for just a function that can take the structure and throw it into an associative array so that I don’t have to recode the parser every time the data format changes?
So, my search began anew. This time, I found something that was perfect: two functions that could either take an associative array and dump out an XML string or take an XML string and dump out an associative array. I was amazed since it was exactly what I was looking for. My pain relief came from XML @=@ Associative Array.
Frankly, it looks like ignored code. It’s been around for nearly seven years on that site, yet it hasn’t even had 8000 views? What is wrong with people? IMHO, this type of functionality should be directly built into the core of PHP. Now I admit that this code isn’t everyone’s cup-o-tea, but it should work for about 95% of people that just want to use the data in an XML file quickly and easily into their code.
The code itself isn’t perfect, it doesn’t do enough checking, so it throws massive amounts of warnings. I also found the naming schemes a bit lacking and some of the formatting not quite to my liking. So, I cleaned it up and modified some of the naming conventions. While I was at it, I added the ability to load XML from a file or URL rather than just from a string. Apparently, this was intended based on the given examples but was never implemented. The code is a bit long to post here, so click the download link below to get the file.
WordPress autosave fails me again. I had expanded this post by more than 100% with examples and details of the parameter options for each classes’ methods. Unfortunately, Flash crashed my browser. When I loaded back up, all my changes were nowhere to be found.
I guess that the autosave doesn’t work when you are updating a post. This really should be a feature where modifying a post that has already been published causes new revisions to be saved but not made active. This way I could recover data in the event of a browser crash.
Anyways. I updated the download above. The xmlLib.php file has been updated (check the file for update notes) and I’ve included a test file that will show you a quick example of how to use the two classes.
Did I help you? Send me a tip.