PHP DOM 与 SimpleXML 的 Atom GData feed 解析
我正在构建一个库来访问 Google Analytics 数据导出 API。该库访问的所有数据都是 Atom 格式,并始终使用许多不同的命名空间。到目前为止,我对 API 的实验一直使用 SimpleXML 进行解析,特别是因为我所做的只是访问提要中保存的数据。
现在我要编写一个库,我想知道使用 SimpleXML 是否足够,或者 PHP 中 DOM 模块的增强功能是否会在未来受益。我还没有为库的这一部分编写太多代码,因此选择仍然是开放的。
我读到,如果您需要动态构建 XML DOM 或修改现有的 DOM,PHP DOM 模块可能是更好的选择,但由于 API 的性质,我不完全确定我是否需要该功能(例如,不将数据推送到服务器)。 SimpleXML 当然更容易使用,我见过有人说,对于只读情况,它就是您所需要的。
本质上问题是,你会使用什么?兼容性不会成为问题,因为服务器配置将满足应用程序的要求。构建库时考虑 PHP DOM 是否值得,还是我现在应该坚持使用 SimpleXML?
更新:以下是我将要处理的 Feed 类型的两个示例:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的看法:
SimpleXML 优点:
SimpleXML 缺点:
DOM 优点:
DOM 缺点:
我个人会一直坚持使用 DOM。它会带您去任何您需要去的地方。文档已经足够了,尽管您的手可能会因打字而酸痛,但您永远不会沮丧地思考“为什么它不能这样做这个?”
如果您需要做的只是阅读文档或进行一些非常基本的编辑,那么可能会选择 SimpleXML。然而对我来说,我已经习惯了 DOM 库,所以我随处使用它,这很棒。
My take on it:
SimpleXML pros:
SimpleXML cons:
DOM pros:
DOM cons:
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 完全能够处理命名空间。在您的情况下,您只需迭代条目,使用
getNameSpaces
并将参数传递给children
方法即可请求具有特定命名空间的子项。也就是说,如果您想编写 3 倍的代码,则可以使用 DOM。
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 thechildren
method to request children with a certain namespace.That said, you can use DOM if you want to write 3x the code.
好吧,如果您需要重新发明轮子,我可能会使用 SimpleXML...或者您可以只使用 Zend_Gdata(如果你需要的东西在那里)或扩展Zend_Feed_Atom 如果不是。任一选项都可能是您需要完成的工作量的一半:)
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 :)