I was very excited today while glancing through the code in ext/simplexml/simplexml.c to find some, as of yet, undocumented methods in PHP’s SimpleXMLElement class. This discovery came after I’ve spent several hours over the last couple of nights banging my head against the desk to figure out a way to create a class that extends SimpleXMLElement and adds a new method for adding a child, which would have to use DOM in order to work – or so I thought.
During this process, I’ve discovered two major limitations of SimpleXML:
- You cannot override the SimpleXMLElement constructor; it is a
finalmethod in the internals
- You can extend SimpleXMLElement and add new methods, but looping through child nodes creates new SimpleXMLElement objects that do not use your class and, thus, cannot use your methods
Nevertheless, I can get over these limitations after discovering these new SimpleXMLElement methods in PHP 5.1.4. In particular, the last three are what interest me most (
these are not yet documented in the PHP manual ):
Previously, one of the biggest limitations of SimpleXML was that you could not add new child nodes to an XML document. Adding attributes to an element was no problem, but in order to add a child element, you had to go a round-about way through importing the SimpleXML object to DOM with
dom_import_simplexml(), adding the child element, and importing the DOM object back to SimpleXML with
simplexml_import_dom(). This was messy and anything but simple.
Now, it’s clean and simple, as SimpleXML should be: