Objeyi XML’ye Çevirme Sınıfı

<?php
class objeXML {
   private $obje;
   public function __construct($objeler) {
      $this->obje = new DOMDocument("1.0", "UTF8");
      $ana = $this->obje->createElement(get_class($objeler));
      foreach($objeler as $anahtar=>$deger) {
        $bagla = $this->baglanti($anahtar, $deger);
        if($bagla != NULL) $ana->appendChild($bagla);
      }
      $this->obje->appendChild($ana);
   }
  private function baglanti($anahtar, $deger){
  	$bagla = NULL;
  	if(is_string($deger) || is_numeric($deger) || is_bool($deger) || $deger == NULL) {
  	  if($deger == NULL) $bagla = $this->obje->createElement($anahtar);
			else $bagla = $this->obje->createElement($anahtar, (string)$deger);
  	}else{
			$bagla = $this->obje->createElement($anahtar);
			if($deger != NULL) {
				foreach($deger as $anahtar=>$deger) {
					$on = $this->baglanti($anahtar, $deger);
					if($on != NULL)  $bagla->appendChild($on);
				}
			}
  	}
		return $bagla;
  }
   public function __toString() {
    return $this->obje->saveXML();
  }
}
$ornekveri=(object)["yazar"=>(object)["ulusan"=>(object)["yazilim"=>"ulusan"]]];
$x=new objeXML($ornekveri);
echo $x;
?>

Çıktısı

<?xml version="1.0" encoding="UTF8"?>
<stdClass>
    <yazar>
        <ulusan>
            <yazilim>ulusan</yazilim>
        </ulusan>
    </yazar>
</stdClass>