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>