How to generate XML file dynamically using PHP ?

How to generate XML file dynamically using PHP ?

Asked on December 20, 2018 in XML.
Add Comment


  • 3 Answer(s)

    To generate xml file by php:

    <?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());
    

    Also use SimpleXMLElement.

    Answered on December 20, 2018.
    Add Comment

    Simplest solution:

         To build an XMLdocument in PHP , we should reason a DOMDocument class, create child nodes and attach these nodes in the correct branch of the document tree.

    Follow the code below:

    1. At line 2 create an empty xml document just specify xml version (1.0) and encoding (utf8).
    2. To populate the xml tree:
    • Create an xmlnode line 5.
    • and append in the correct position. Append this directly to the domdocument because of creating the root.
    • 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.

    We can create and append a node in just a line 13th. For example, we can do a lot of other things with the dom api.

    <?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);
     
      $currentTrack = $domtree->createElement("track");
      $currentTrack = $xmlRoot->appendChild($currentTrack);
     
      /* you should enclose the following two lines in a cicle */
      $currentTrack->appendChild($domtree->createElement('path','song1.mp3'));
      $currentTrack->appendChild($domtree->createElement('title','title of song1.mp3'));
     
      $currentTrack->appendChild($domtree->createElement('path','song2.mp3'));
      $currentTrack->appendChild($domtree->createElement('title','title of song2.mp3'));
     
      /* get the xml printed */
      echo $domtree->saveXML();
    ?>
    

    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

    Answered on December 20, 2018.
    Add Comment

    Easy way:

         The below code should be saved as .php

    <?php
    // Send the headers
    header('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>';
     
    ?>
    
    Answered on December 20, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.