Extending SimpleXML

September 3, 2005 by Oliver
Filed under: PHP, Web Programming 

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]]>

Comments

No Comments on Extending SimpleXML

  1. Oliver on Sat, 3rd Sep 2005 6:45 pm
  2. < ![CDATA[A working (but limited) Microformats extension to SimpleXML:

    MicroformatXML.]]>

  3. Oliver on Sat, 3rd Sep 2005 6:58 pm
  4. < ![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.]]>

  5. Windsurf on Mon, 7th Nov 2005 6:06 pm
  6. < ![CDATA[I would really like to see a working example. The thing I'm trying to do, is have two classes that extend simplexml, and store extra member properties. There is one xml file, say addressBook is the root node. Inside there are several 'person' nodes. I want to have one class handle the addressbook as a whole, and another class handle the person nodes as objects. One method of the person object would have a method like getName() and it would join firstName and lastName attributes and return them.

    I am a bit foggy on how to do this with your class.]]>

  7. Oliver on Thu, 17th Nov 2005 8:54 pm
  8. < ![CDATA[As it stood, it wasn't easily possible...

    It should be now.

    Enhanced ExtendXML]]>

  9. helga on Sat, 17th Dec 2005 3:16 pm
  10. sounds like just what i need.. got an example of how to use it?

  11. Rick Jolly on Tue, 20th Feb 2007 6:45 pm
  12. I think what you have created is a wrapper class for simplexml.

  13. Ze Thriller on Mon, 2nd Jul 2007 2:59 pm
  14. < ![CDATA[I've tested your project and it seemed some advances functions for manipulating attributes/children nodes didn't work very well. So I've created my own class (XXS) based on yours and built a real documentation with it.

    I'd like to publish it on sourceforge.net but i didn't see anything related to the license under whom the project ExtendXML is... so I don't know if I have the right to do this.
    Of course your name and your website will be provided directly in the source code.

    Please e-mail me as an answer :)]]>

  15. Oliver on Tue, 3rd Jul 2007 5:40 pm
  16. Just for clarification, if anybody finds ExtendXML useful, you can do with it what you like :)

    [...] ribute. Problem is, it won&#8217;t work with objects, arrays or resources. Someone already tried to do it. Sorry Oliver Brown, but I don&#8217;t like your solution. I shall th [...]

    [...] schuetzki, 2007-05) Probleme und Wissenwertes beim Extenden von SimpleXML Overloading 1: Extending SimpleXML, Issues with extending SimpleXML, Microformats extension to Simp [...]

  17. Priyan on Sat, 10th May 2008 6:00 pm
  18. < ![CDATA[Hai
    I am doing an exercise in Simple XML which loads the xml doc and do some query. When i tried a query to find all universities, it will give the list of universities.
    Now i have to click one university name and i have to go to its own details which are stored in the xml will be retieved.
    i could not get it, my code gives error.
    also i could not pass the session variable to go the other page!
    What can i do]]>