PHP的DOM和SimpleXML扩展之间有什么区别? [英] What's the difference between PHP's DOM and SimpleXML extensions?

查看:182
本文介绍了PHP的DOM和SimpleXML扩展之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解为什么我们在PHP中需要2个XML解析器.

I'm failing to comprehend why do we need 2 XML parsers in PHP.

有人可以解释两者之间的区别吗?

Can someone explain the difference between those two?

推荐答案

简而言之:

SimpleXml

  • 用于简单的XML和/或简单的UseCases
  • 有限的API可与节点一起使用(例如,无法对接口进行太多编程)
  • 所有节点属于同一种类(元素节点与属性节点相同)
  • 节点是魔术般可访问的,例如$root->foo->bar['attribute']
  • is for simple XML and/or simple UseCases
  • limited API to work with nodes (e.g. cannot program to an interface that much)
  • all nodes are of the same kind (element node is the same as attribute node)
  • nodes are magically accessible, e.g. $root->foo->bar['attribute']

DOM

  • 适用于您可能拥有的任何XML用例
  • 是W3C DOM API的实现(发现有多种语言实现)
  • 区分各种节点类型(更多控制权)
  • 由于显式API(可以对接口进行编码),因此更加冗长
  • 可以解析损坏的HTML
  • 允许您在XPath查询中使用PHP函数
  • is for any XML UseCase you might have
  • is an implementation of the W3C DOM API (found implemented in many languages)
  • differentiates between various Node Types (more control)
  • much more verbose due to explicit API (can code to an interface)
  • can parse broken HTML
  • allows you to use PHP functions in XPath queries

这两种方法均基于 libxml ,并且受

Both of these are based on libxml and can be influenced to some extend by the libxml functions

个人,我不太喜欢SimpleXml.那是因为我不喜欢隐式访问节点,例如$foo->bar[1]->baz['attribute'].它将实际的XML结构与编程接口联系在一起.一切都是单节点类型也有些不直观,因为SimpleXmlElement的行为根据其内容而神奇地改变.

Personally, I dont like SimpleXml too much. That's because I dont like the implicit access to the nodes, e.g. $foo->bar[1]->baz['attribute']. It ties the actual XML structure to the programming interface. The one-node-type-for-everything is also somewhat unintuitive because the behavior of the SimpleXmlElement magically changes depending on it's contents.

例如,当您拥有<foo bar="1"/>时,/foo/@bar的对象转储将与/foo的对象转储相同,但是对它们进行回显将显示不同的结果.而且,由于它们都是SimpleXml元素,因此您可以在它们上调用相同的方法,但是只有在SimpleXmlElement支持它的情况下才能应用它们,例如尝试在第一个SimpleXmlElement上执行$el->addAttribute('foo', 'bar')不会执行任何操作.现在,当然可以,不能将属性添加到属性节点",这是正确的,但是,要点是,属性节点首先不会公开该方法.

For instance, when you have <foo bar="1"/> the object dump of /foo/@bar will be identical to that of /foo but doing an echo of them will print different results. Moreover, because both of them are SimpleXml elements, you can call the same methods on them, but they will only get applied when the SimpleXmlElement supports it, e.g. trying to do $el->addAttribute('foo', 'bar') on the first SimpleXmlElement will do nothing. Now of course it is correct that you cannot add an attribute to an Attribute Node, but the point is, an attribute node would not expose that method in the first place.

但这只是我的2分.下定决心:)

旁注上,没有两个解析器,但是

On a sidenote, there is not two parsers, but a couple more in PHP. SimpleXml and DOM are just the two that parse a document into a tree structure. The others are either pull or event based parsers/readers/writers.

也请参阅我的答案

这篇关于PHP的DOM和SimpleXML扩展之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆