PHP DOM 与 SimpleXML 的 Atom GData feed 解析

发布于 2024-09-02 17:04:47 字数 843 浏览 10 评论 0原文

我正在构建一个库来访问 Google Analytics 数据导出 API。该库访问的所有数据都是 Atom 格式,并始终使用许多不同的命名空间。到目前为止,我对 API 的实验一直使用 SimpleXML 进行解析,特别是因为我所做的只是访问提要中保存的数据。

现在我要编写一个库,我想知道使用 SimpleXML 是否足够,或者 PHP 中 DOM 模块的增强功能是否会在未来受益。我还没有为库的这一部分编写太多代码,因此选择仍然是开放的。

我读到,如果您需要动态构建 XML DOM 或修改现有的 DOM,PHP DOM 模块可能是更好的选择,但由于 API 的性质,我不完全确定我是否需要该功能(例如,不将数据推送到服务器)。 SimpleXML 当然更容易使用,我见过有人说,对于只读情况,它就是您所需要的。

本质上问题是,你会使用什么?兼容性不会成为问题,因为服务器配置将满足应用程序的要求。构建库时考虑 PHP DOM 是否值得,还是我现在应该坚持使用 SimpleXML?

更新:以下是我将要处理的 Feed 类型的两个示例:

  1. 帐户 Feed
  2. 数据源

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无尽的现实 2024-09-09 17:04:47

我的看法:

SimpleXML 优点:

  • 简单
  • 半直观的非详细代码
  • 非常适合阅读/解析文档

SimpleXML 缺点:

  • 不适合生成 XML。
  • 缺乏灵活性和功能

DOM 优点:

  • 非常强大
  • 非常适合生成文档
  • 如果您做过任何其他 DOM 脚本编写(例如:在 Javascript 中),就会很熟悉

DOM 缺点:

  • 冗长
  • 有时不必要地复杂。

我个人会一直坚持使用 DOM。它会带您去任何您需要去的地方。文档已经足够了,尽管您的手可能会因打字而酸痛,但您永远不会沮丧地思考“为什么它不能这样做这个?”

如果您需要做的只是阅读文档或进行一些非常基本的编辑,那么可能会选择 SimpleXML。然而对我来说,我已经习惯了 DOM 库,所以我随处使用它,这很棒。

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.

雅心素梦 2024-09-09 17:04:47

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 the children method to request children with a certain namespace.

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

贪了杯 2024-09-09 17:04:47

好吧,如果您需要重新发明轮子,我可能会使用 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文