How to Write XML Files in PHP

How to Write XML Files in PHP thumbnail
PHP scripts within Web applications can write files in formats such as XML.

PHP scripts can write files of many different types, including XML. Using PHP to write XML files is straightforward and something anyone can learn to do, even without a lot of programming experience. If you make sure you have a clear idea of what structure you want the XML to reflect before you set about writing your PHP code to output it to a file, you will make the process easier and less prone to error. Both PHP and XML are accessible technologies for developers at any stage.

Instructions

    • 1

      Create your PHP script by opening a new file in your text editor or Integrated Development Environment and saving it with ".php" extension, for example "writexml.php" or another name relevant to your purposes. Enter the PHP outline:

      <?php

      //code goes here

      ?>

      This is the basic structure of any PHP script. Between the opening and closing PHP tags, you can enter any PHP code you need to process and write your XML file.

    • 2

      Create your XML structures. Work out whatever XML you need before attempting to write it out from your PHP code, as this will minimize the likelihood of error. If you are new to XML, make sure you understand how it is structured. XML uses tree-structured data, with elements delineated by tags, as in the following example:

      <?xml version='1.0'?>

      <individual>

      <title>Dave</title>

      <years>12</years>

      </individual>

      Create a sample of the XML content you need in a separate file for reference while you're developing your PHP code.

    • 3

      Build the XML structure into your PHP script. If your XML is going to be built dynamically, which is the most common situation in Web programming, capture or process whatever data you need included and build it into the XML. The following example PHP code shows data being captured from get variables:

      $individual_title = $_GET['ititle'];

      $individual_years = $_GET['iyears'];

      $xml_text = "<?xml version='1.0'?><individual>";

      $xml_text .= "<title>".$individual_title."</title>";

      $xml_text .= "<years>".$individual_years."</years>";

      $xml_text .= "</individual>";

      The "$xml_text" variable now contains the entire XML structure to be written out, including those items of data passed from the get variables. Get variables allow you to pass data from one page to another, by appending that data to the page address.

    • 4

      Add code to open your XML file for writing, or create it if it does not already exist (after the XML text has been stored in the variable):

      //get a handler for the file

      $file_handler = fopen("filename.xml", 'a');

      //write the XML

      fwrite($file_handler, $xml_text);

      //close the file handler

      fclose($file_handler);

      Change the file name to suit your needs. This code is for a file in the same directory as the PHP script, so if you want it to be in a different location, include that instead.

    • 5

      Upload your PHP script to a Web server and test it. To run the PHP script, you can browse to it in a Web browser, including the get variables, for example by typing a URL with the following sample structure into your browser address bar:

      http://yourdomain.com/writexml.php?ititle=Dave&iyears=12

      Once you have browsed to the script once, you can check whether the XML file has been written to your server, as well as whether it contains the correct content and is properly structured.

Tips & Warnings

  • If your PHP script for writing an XML file is being called from another script, remember to pass the get variables from there.

  • Resist the urge to use XML structures without validating them. You can use free Web tools to do this, identifying any errors you have not noticed.

Related Searches

References

Resources

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

You May Also Like

Related Ads

Featured
View Mobile Site