How to generate XML file dynamically using PHP? How to generate XML file dynamically using PHP? xml xml

How to generate XML file dynamically using PHP?


I'd use SimpleXMLElement.

<?php$xml = new SimpleXMLElement('<xml/>');for ($i = 1; $i <= 8; ++$i) {    $track = $xml->addChild('track');    $track->addChild('path', "song$i.mp3");    $track->addChild('title', "Track $i - Track Title");}Header('Content-type: text/xml');print($xml->asXML());?>

$xml->asXML() can also take a filename as a parameter to save to that file


To create an XMLdocument in PHP you should instance a DOMDocument class, create child nodes and append these nodes in the correct branch of the document tree.

For reference you can read http://it.php.net/manual/en/book.dom.php

Now we will take a quick tour of the code below.

  • at line 2 we create an empty xml document(just specify xml version (1.0) and encoding (utf8))
  • now we need to populate the xml tree:
    • We have to create an xmlnode (line 5)
    • and we have to append this in the correct position. We are creating the root so we append this directly to the domdocument.
    • Note create element append the element to the node and return the node inserted, we save this reference to append the track nodes to the root node (incidentally called xml).

These are the basics, you can create and append a node in just a line (13th, for example), you can do a lot of other things with the dom api. It is up to you.

<?php        /* create a dom document with encoding utf8 */    $domtree = new DOMDocument('1.0', 'UTF-8');    /* create the root element of the xml tree */    $xmlRoot = $domtree->createElement("xml");    /* append it to the document created */    $xmlRoot = $domtree->appendChild($xmlRoot);    /* you should enclose the following lines in a cicle */    $currentTrack = $domtree->createElement("track");    $currentTrack = $xmlRoot->appendChild($currentTrack);    $currentTrack->appendChild($domtree->createElement('path','song1.mp3'));    $currentTrack->appendChild($domtree->createElement('title','title of song1.mp3'));    $currentTrack = $domtree->createElement("track");    $currentTrack = $xmlRoot->appendChild($currentTrack);    $currentTrack->appendChild($domtree->createElement('path','song2.mp3'));    $currentTrack->appendChild($domtree->createElement('title','title of song2.mp3'));    /* get the xml printed */    echo $domtree->saveXML();?>

Edit:Just one other hint:The main advantage of using an xmldocument (the dom document one or the simplexml one) instead of printing the xml,is that the xmltree is searchable with xpath query


An easily broken way to do this is :

<?php// Send the headersheader('Content-type: text/xml');header('Pragma: public');header('Cache-control: private');header('Expires: -1');echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";echo '<xml>';// echo some dynamically generated content here/*<track>    <path>song_path</path>    <title>track_number - track_title</title></track>*/echo '</xml>';?>

save it as .php