Create A XML File Using PHP

1. Create File createxmlExample.php

<?php

$xmlnew DomDocument("1.0","UTF-8");
$xml->formatOutput=true;
$xml->preserveWhiteSpace=false;
$company=$xml->createElement("company");

    $company=$xml->createElement("company");
    $xml->appendChild($company);

    $employee=$xml->createElement("employee");
    $company->appendChild($employee);

    $name=$xml->createElement("name","John");
    $employee->appendChild($name);

    $age=$xml->createElement("age",25);
    $employee->appendChild($age);

    $department=$xml->createElement("department","computer");
    $employee->appendChild($department);
    
    echo "<xmp>".$xml->saveXML()."</xmp>";
    
    $xml->save("company.xml");

?>

2. Save the about code and run the createxmlExample.php in you browser


3. Now you can see the generated xml  file in your project file as company.xml



Output :

<?xml version="1.0" encoding="UTF-8"?>
<company>
  <employee>
    <name>John</name>
    <age>25</age>
    <department>computer</department>
  </employee>
</company>




No comments:

Post a Comment