Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Generate xml file in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 364
    Comment on it

    If we need to export our application data in XML format then we can generate an XML file with the use of PHP.

    Let's see how can we do it.

    first of all, in our PHP file create a class that initializes the parameters.

     

    class Xmltest
    {
      public $author;
      public $title;
      public $date;
      public $categories;
     
      function __construct($author, $title, $date, $categories)
      {
        $this->author = $author;
        $this->title = $title;
        $this->date = $date;
        $this->categories = $categories;
      }
    }

     

    We have created a class "Xmltest" which has public members parameters $author, $title, $date and $categories. The constructor of is to initialize the parameters.

    Next, we will create an object of the class and pass values to them.

     

    $xmltest = array(
      new Xmltest(
        "AYN RAND",
        "Ayn Rand Institute",
        "1/1/2016",
        array(
          "We the Living",
          "The Fountainhead",
          "Atlas Shrugged"
        )
      ),
       
      new Xmltest(
        "Ernest Hemingway",
        "Hemingway",
        "1/1/2016",
        array(
          "Indian Camp",
          "The Sun Also Rises",
          "Death in the Afternoon"
        )
      )
    );
    
    

     

    Now create an object of the XML document $xmlDoc, with the use of this object creates the root element as "RecentBooks".

     

    $root = $xmlDoc->appendChild(
              $xmlDoc->createElement("RecentBooks"));
    
    

     

    then for the child element make a foreach loop of $xmltest in which create the author's attribute and title, date's element and then to make the output pretty make formatoutput true as

     

    $xmlDoc->formatOutput = true;

     

    putting it all together we have the XML file export code as below :

     

    <?php
    
    class Xmltest
    {
      public $author;
      public $title;
      public $date;
      public $categories;
     
      function __construct($author, $title, $date, $categories)
      {
        $this->author = $author;
        $this->title = $title;
        $this->date = $date;
        $this->categories = $categories;
      }
    }
    
    $xmltest = array(
      new Xmltest(
        "AYN RAND",
        "Ayn Rand Institute",
        "1/1/2016",
        array(
          "We the Living",
          "The Fountainhead",
          "Atlas Shrugged"
        )
      ),
       
      new Xmltest(
        "Ernest Hemingway",
        "Hemingway",
        "1/1/2016",
        array(
          "Indian Camp",
          "The Sun Also Rises",
          "Death in the Afternoon"
        )
      )
    );
    
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Disposition: attachment;filename=$filename");
    header("Content-Transfer-Encoding: binary ");
    
    $xmlDoc = new DOMDocument();
    
    $root = $xmlDoc->appendChild(
              $xmlDoc->createElement("RecentBooks"));
           
    
    foreach($xmltest as $_xmltest)
    {
      $xmlTag = $root->appendChild(
                  $xmlDoc->createElement("Books"));
               
      $xmlTag->appendChild(
        $xmlDoc->createAttribute("author"))->appendChild(
          $xmlDoc->createTextNode($_xmltest->author));
       
      $xmlTag->appendChild(
        $xmlDoc->createElement("Title", $_xmltest->title));
       
      $xmlTag->appendChild(
        $xmlDoc->createElement("Date", $_xmltest->date));
    
      $catTag = $xmlTag->appendChild(
                  $xmlDoc->createElement("Novels"));
    
      foreach($_xmltest->categories as $cat)
      {
        $catTag->appendChild(
          $xmlDoc->createElement("Novels", $cat));
      }
    }
    
    
    header("Content-Type: text/plain");
    
    
    $xmlDoc->formatOutput = true;
    
    
    if($xmlDoc->save('exemple_xml.xml')) echo $xmlDoc->saveXML();
    else  echo 'Error: the example_dom.xml cannot be created';
    
    ?>
    
    

     

    In this way, we can generate XML file of our application data in PHP

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: