mail usprint this pagerss feed

www.liip.ch

Liip is hiring!

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.

Related Entries:
Added DOMNode::getNodePath
domxml and PHP 5
Check your XML catalogs ....
Any missing XML features in PHP?
Comments (2) |  Permalink

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...
eduardo spotorno @ 27.11.2006 03:20 CEST
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!

For Spammers Only
Name*
E-Mail
URL
Comment*
Notify me via E-Mail when new comments are made to this entry
Remember me (needs cookies)