views:

295

answers:

3

I'm building a library to access the Google Analytics Data Export API. All the data the library accesses is in Atom format and utilises numerous different namespaces throughout. My experiments with the API have used SimpleXML for parsing so far, especially as all I have been doing is accessing the data held within the feed.

Now I'm coming to write a library I am wondering whether forging ahead with SimpleXML will be adequate or whether the enhanced functionality of the DOM module in PHP would be of benefit in the future. I haven't written much code for this part of the library yet so the choice is still open.

I have read that the PHP DOM module can be a better choice if you need to build an XML DOM on the fly or modify an existing one, but I'm not entirely sure I would need that functionality anyway due to the nature of the API (no pushing data to the server, for instance). SimpleXML is certainly easier to use and I have seen people saying that for read-only situations it is all you need.

Essentially the question is, what would you use? Compatibility will not be an issue as the server configuration will match the application's requirements. Is it worth building the library with PHP DOM in mind or should I stick with SimpleXML for now?

Update: Here are two examples of the kind of feeds I will be dealing with:

  1. Account feed
  2. Data feed
A: 

SimpleXML is perfectly capable of dealing with namespaces. In your case you just have to iterate through the entries, use the getNameSpaces and pass an argument to the children method to request children with a certain namespace.

That said, you can use DOM if you want to write 3x the code.

Artefacto
Yes, that is pretty much what I am doing now. I'm more worried about a situation where I will need to do some DOM manipulation and/or creation.
Geoff Adams
+4  A: 

My take on it:

SimpleXML pros:

  • Simple
  • Semi-intuitive non-verbose code
  • Great for reading/parsing documents

SimpleXML cons:

  • Terrible for generating XML.
  • Lacking in flexibility and power

DOM pros:

  • Tremendously powerful
  • Great for generating documents
  • Will be familiar if you've done any other DOM scripting (eg: in Javascript)

DOM cons:

  • Verbose
  • Sometimes needlessly complicated.

I personally would stick to DOM all the way. It will get you anywhere you need to go. The documentation is more than adequate, and although your hands might get sore from all the typing, you'll never get frustrated thinking "why can't it just do this??"

If all you need to do is read a document, or do some very basic editing, then probably go with SimpleXML. For me however, I'm used to the DOM libraries, so I just use that everywhere and it's great.

nickf
A: 

Well if you need to reinvent the wheel, i would probably use SimpleXML... Or you could just use the Zend_Gdata (if what you need is in there) or extend Zend_Feed_Atom if its not. Either option should probably half the amount of work you need to do :)

prodigitalson
Yes, there is some great code on GitHub by someone who submitted a Google Analytics module for Zend_Gdata, but in my case I am using OAuth as the authentication layer and as far as I know it isn't fully supported for Zend_Gdata as of yet. I could be wrong though - feel free to correct me!How is Zend_Feed_Atom with namespaced elements? I presume it copes perfectly fine?
Geoff Adams
It should handle namespaces just find. The Zend_Feed base class has registerNamespace and lookupNamespace methods which i assume are similar in thier workings to the namespace management ive had to write on some of my SimpleXML based classes in the past.
prodigitalson