What DOMNodes can be represented by SimpleXMLElement?
The biggest difference between the two libraries is that SimpleXML is basically one class: SimpleXMLElement . In contrast, the DOM extension has many classes, most of which are a subtype of DOMNode .
So, one key question when comparing these two libraries is which of the many offers of DOM classes can be represented at the end of SimpleXMLElement ?
The following is a comparison table containing those DOMNode types that are really useful when it comes to XML (useful node types). Your mileage may vary, for example. when you need to deal with DTD, for example:
+-------------------------+----+--------------------------+-----------+ | LIBXML Constant |
As shown in this table, SimpleXML has really limited interfaces compared to the DOM. Along with those in the table, SimpleXMLElement also abstracts access to child elements and attribute lists, and also provides traversal through element names (access to properties), attributes (array access), as well as Traversable , iterating its "own" children (elements or attributes) and offering access to the namespace using the children() and attributes() methods.
As long as this whole magical interface is beautiful, however, it cannot be changed by extension from SimpleXMLElement, as well as magic, as well as limited.
To find out what type of nodetype a SimpleXMLElement is, see below:
- How to tell about all SimpleXML objects representing an element and attribute?
DOM follows DOMDocument Core Level 1 specifications here . You can do almost every conceivable XML processing with this interface. However, this is only level 1, so compared to modern DOMDocument levels like 3, it is somewhat limited for some cooler things. I am sure that SimpleXML is also lost here.
SimpleXMLElement allows you to cast subtypes. This is very important in PHP. The DOM also allows this, although a bit more, and you need to choose a narrower type.
XPath 1.0 is supported by both, the result in SimpleXML is an array from SimpleXMLElements , in DOM a DOMNodelist .
SimpleXMLElement supports listing for string and array (json), the DOMNode classes in the DOM do not. They offer casting to an array, but only like any other object (public properties like keys / values).
Common patterns for using these two extensions in PHP:
- Usually you use SimpleXMLElement. The level of knowledge of XML and XPath is equally low.
- After struggling with the magic of their interfaces, sooner or later a certain level of frustration will be reached.
- You will find that you can import
SimpleXMLElement into the DOM and vice versa. You will learn more about the DOM and how to use the extension to do what you failed (or failed to figure out) with SimpleXMLElement . - You noticed that you can load HTML documents with the DOM extension. And invalid XML. And format the output. Things SimpleXMLElement just can't do. Even with dirty tricks.
- You might even completely switch to the DOM extension, because at least you know that the interface is more differentiated and allows you to do something. You also see an advantage in learning the DOM 1 level, because you can use it also in Javascript and other languages โโ(a huge advantage of the DOM extension for many).
You can enjoy both extensions, and I think you should know both. The bigger, the better. All libxml-based extensions in PHP are very good and powerful extensions. And on Stackoverflow under php there is a good tradition to cover these libraries well, as well as detailed information.