PHP, Web Programming

Extending SimpleXML

<![CDATA[I posted a while ago about problems with extending SimpleXML (which I intend to do so I can add automatic microformat support). Well I think I’ve come up with a solution.

I’ve created a class called ExtendXML that is completely extendable (see the other post for an explanation of why SimpleXML isn’t extendable) that provides all of the functionality of SimpleXML.

The initial call is similar to using SimpleXML (except I made the functions static class methods instead):

ExtendXML::load_string($xmlString, [$class, [$simpleClass]]);
ExtendXML::load_file($xmlFile, [$class, [$simpleClass]]);

The $class argument contains the name of the class that will be returned. This should extend ExtendXML and defaults to ExtendXML.

The $simpleClass argument contains the name of the SimpleXML class that will be passed to SimpleXML functions. This should extend SimpleXMLElement and defaults to SimpleXMLElement.

The object returned can then be used the same as a SimpleXMLElement object.

I haven’t tested it much or documented it yet, but it supports child tags (returning objects are of the class ExtendXML or whatever you specified), attributes and xpath expressions.


PHP5, OOP, ArrayAccess, SPL, programming, code, tutorial, microformats, XML, SimpleXML]]>

11 thoughts on “Extending SimpleXML

  1. Pingback: Oliver
  2. <![CDATA[ExtendXML::load_* now accept a fourth argument, $removeNameSpace that defaults to false.

    When set to true it removes any default namespace declaration in the XML since SimpleXML doesn’t play nicely with them. Although this is supposedly fixed in PHP 5.1.]]>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s