I'd like to build an object-oriented toolkit for PHP to make creating and/or parsing XML much simpler, but before I spend any time seriously developing it, I wanted to see if someone else has already built a better wheel for the same purpose.

I'd envision something where a class would represent a opening and closing pair of tags, and providing an xml string would return a hierarchy of objects:

// a simplified example, but this would represent '<order id="A1">Lorem ipsum</order>'
class XmlClass {
  $name = 'order';
  $content = 'Lorem ipsum';
  $attributes = array('id' => 'A1');
  $children = array(); // this would child tag objects of this same class
}

Using this kind of interface, you could even provide an entire xml document as the string, and get back a hierarchical structure of class objects.

Additionally, you could directly instantiate a new class, and the use a method of the same class to output the equivalent xml:

// this would (more or less) output '<order id="C6">The quick fox jumps over the lazy dog</order>'
$tag = new XmlClass( 'order', 'The quick fox jumps over the lazy dog', array('id' => 'C6') );
echo $tag->to_string();

What this would accomplish (in theory) is something easy to use like SimpleXML, but potentially much more powerful in the long run. It would allow parsing and manipulation of a single snippet or an entire document, as well as making programmatic xml generation a breeze.

Thoughts? Admonishments, praises, reprimands?

    You might want to consider adding to the SimpleXML functionality by extending the SimpleXMLElement class:

    class XmlClass extends SimpleXMLElement {
    

    Then you'll inherit all the existing SimpleXML functionality which you can then build upon.

    And as of this moment there are 31 XML-related PEAR packages you might want to browse through, perhaps extending them instead (or also).

      On top of that there is also the [man]DOM[/man] class/extension (among others). If there is any "definitive" XML interface that any toolkit is expected to support, it would be the W3C document object model. PHP's [man]DOM[/man], [man]XMLReader[/man], [man]XMLWriter[/man], and [man]SimpleXML[/man] extensions all wrap around the [man]libxml[/man] toolkit.

        Write a Reply...