Reader Feedback: Working with XML In PHP

Welcome to the 3rd installment of me answering reader feedback questions. Today we deal with a topic that I deal with every day at my day job -- working with XML in PHP.

First of all, I do all this in PHP 5. SimpleXML makes it so easy to manipulate XML. For my main work project, I've written a PHP application front end that talks to eXist, an open source XML database. Since we deal with XML all day long it was decided that we needed to eat our own dog food and go with an XML database to store the information that the application generates. So a lot of my code does some of the following things:

  • Do a call to a xQuery script running on the database that returns info that I'm looking for, usually both comma and pipe-separated data
  • Load a specific document into a SimpleXML object and pull out some specific info
  • Load a specific document into a SimpleXML object and then use it's XPath functions to pull some information out.

Those 3 techniques seem to work well for me when dealing with XML data in PHP. If I had to go back and redo some of the earlier code, I'd probably change things a little differently in how I'm using eXist to simulate how some of our other products work:

  1. Use eXist to give me a list of documents that contain the info I am looking for
  2. Load those documents into SimpleXML
  3. Use Xpath to grab the data I needed
But in the interest of Getting Things Done, what I've done works pretty good and the code is really not horrible.