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.

ExtendXML

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

11 responses to “Extending SimpleXML

  1. Pingback: Oliver

  2. Oliver 2005-09-03 at 18:58

    <![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.]]>

    Like

  3. Oliver 2005-11-17 at 20:54

    <![CDATA[As it stood, it wasn't easily possible…

    It should be now.

    Enhanced ExtendXML]]>

    Like

  4. helga 2005-12-17 at 15:16

    sounds like just what i need.. got an example of how to use it?

    Like

  5. Rick Jolly 2007-02-20 at 18:45

    I think what you have created is a wrapper class for simplexml.

    Like

  6. Oliver 2007-07-03 at 17:40

    Just for clarification, if anybody finds ExtendXML useful, you can do with it what you like:)

    Like

  7. Pingback: Web:Extend » Blog Archive » Extending SimpleXML

  8. Pingback: Very SimpleXML - PHPUGFFM - PHP User Group Frankfurt am Main

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: