如何使用PHP将数据从MySQL导出到XML

I have the following PHP code. How would I edit this to export to XML? Please help me, I'm a newbie..

<?php
    $koneksi=mysql_connect("localhost","root","");
    mysql_select_db("abdrahmat",$koneksi);
    $id=77;
    $nmtabel="artikel";
    $sql=mysql_query("SELECT * FROM $nmtabel where id_artikel='$id'");
    $r=mysql_fetch_array($sql);// JavaScript Document

try this:

 $array = json_decode(json_encode($r), true);

 $xml      = new SimpleXMLElement('<root/>');
 $importer = $createArrayImporter($xml);
 echo simplexml_pretty_print($importer($array));