How to extend SimpleXML with missing functionality
Ever thought SimpleXML is too simple? Did you know, you can easily extend the SimpleXMLElement class?
From Rob Richards on the PHP internals mailinglist:
This [getting name and type of a Node] can be done extending the SimpleXMLElement and the functions are a whole 1 line of user code:
<?php
class cNode extends SimpleXMLElement {
function getName() {
return dom_import_simplexml($this)->nodeName;
}
function getType() {
return dom_import_simplexml($this)->nodeType;
}
}
$xml = '<root />';
$sxe = simplexml_load_string($xml, 'cNode');
print $sxe->getName()."n";
print $sxe->getType()."n";
?>
dom_import_simplexml() and simplexml_import_dom() are zero-copy (on the internal dom tree) operations, therefore the performance loss is really negligible.
Comments
markus' blog
@ 01.11.2005 16:37 CEST
(Trackback)
Christian Stocker quoted Rob Richards from the PHP internals mailing list on how to extend SimpleXML. Someone needs more information of the structure of the XML document, for example the name of the node. On the list it was suggested to use the DOM ex...
Christian Stocker quoted Rob Richards from the PHP internals mailing list on how to extend SimpleXML. Someone needs more information of the structure of the XML document, for example the name of the node. On the list it was suggested to use the DOM ex...
im trying to extend simplexml class but denies to accept a constructor because has a "final" statement. is it possible to override this?? thanks!!
add a comment
Your email adress will never be published.
Comment spam will be deleted!
