Creating Simple XML File In PHP

 <?php    
    header("Content-Type: text/plain");

    //create the xml document
    $xmlDoc = new DOMDocument();

    //create the root element
    $root = $xmlDoc->appendChild(
              $xmlDoc->createElement("Students"));

    //make the output pretty
    $xmlDoc->formatOutput = true;

    echo $xmlDoc->saveXML();
?>

Output

<?xml version="1.0"?>
<Students/>

Adding Nodes To XML Document

//create a tutorial element
  $tutTag = $root->appendChild(
              $xmlDoc->createElement("Student"));

  //create the author attribute
  $tutTag->appendChild(
    $xmlDoc->createAttribute("id"))->appendChild(
      $xmlDoc->createTextNode('105'));

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("FirstName", 'Mugil'));

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("LastName", 'Vannan'));

  //create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("Age", '24'));

Whole Code

 <?php    
    header("Content-Type: text/plain");

    //create the xml document
    $xmlDoc = new DOMDocument();

    //create the root element
    $root = $xmlDoc->appendChild(
              $xmlDoc->createElement("Students"));

    //create a tutorial element
  $tutTag = $root->appendChild(
              $xmlDoc->createElement("Student"));

  //create the author attribute
  $tutTag->appendChild(
    $xmlDoc->createAttribute("id"))->appendChild(
      $xmlDoc->createTextNode('105'));

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("FirstName", 'Mugil'));

  //create the title element
  $tutTag->appendChild(
    $xmlDoc->createElement("LastName", 'Vannan'));

  //create the date element
  $tutTag->appendChild(
    $xmlDoc->createElement("Age", '24'));              

    //make the output pretty
    $xmlDoc->formatOutput = true;

    echo $xmlDoc->saveXML();
?>

Output

<?xml version="1.0"?>
<Students>
  <Student id="105">
    <FirstName>Mugil</FirstName>
    <LastName>Vannan</LastName>
    <Age>24</Age>
  </Student>
</Students>
    mysql_connect('localhost', 'root', '');
    mysql_select_db('sampleDB');
    
    $strSQL = 'SELECT * 
                 FROM tbAmenities 
                LIMIT 10';
                         
    $Result = mysql_query($strSQL); 

    $Response = array();
    $Posts    = array();
    
    while($row=mysql_fetch_array($Result)) 
    { 
        $id             =    $row['id']; 
        $amenityname    = $row['amenityname']; 
        
        $amenity[] = array('id'=> $id, 'amenity'=> $amenityname);    
    } 

    $Response['posts'] = $amenity;

    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($Response));
    fclose($fp);