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:
- Account feed
- Data feed
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.
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.
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 :)
精彩评论