Converting an object into XML

windows

Guest
if I have a class that looks like the following:


class myObject
{
private listOtherObjects;

public function __construc() {
$this->listOtherObjects = new Array();
}

public function addOtherObject($o) {
$this->listOtherObjects[] = $o;
}
}

What I would like is to have a method / function that would convert an instance of this class into xml which looks somewhat like the following:

<myObject>
<listOtherObjects>
<otherObject />
</listOtherObjects>
</myObject>


What would be the best way to go about acheiving this?You might be able to come up with something using php5's reflection API. Sorry, ive never really looked at it, but it might be a place to start your search.You should use WDDX (<!-- m --><a class="postlink" href="http://uk.php.net/wddx">http://uk.php.net/wddx</a><!-- m -->) (PEAR Module (<!-- m --><a class="postlink" href="http://pear.php.net/package/XML_Wddx">http://pear.php.net/package/XML_Wddx</a><!-- m -->)) it is an XML standard and serializers exist in many languages.

If you must do it manually, you can use the DOM (<!-- m --><a class="postlink" href="http://www.php.net/dom">http://www.php.net/dom</a><!-- m -->) API to create the XML and use the class and object (<!-- m --><a class="postlink" href="http://uk.php.net/manual/en/ref.classobj.php">http://uk.php.net/manual/en/ref.classobj.php</a><!-- m -->) functions to get the information you need about the obhject.
 
Back
Top