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.
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:
//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.
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:
Create a sample of the XML content you need in a separate file for reference while you're developing your PHP code.
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.
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
//close the 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.
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:
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.
- Photo Credit Hemera Technologies/Photos.com/Getty Images
How to Write Into XML File Using PHP
XML is a Web design standard that uses HTML to order data. For some programmers, the ability to print out XML files...
How to Create an XSD From an Existing XML
Then describe each XML tag as an element within the markup. The element can either be a primitive type, declared in the...
How to Run a Script File in Java to Output an XML File
Creating XML files is used by programmers who want to create dynamic pages from Java code. XML files can be used to...
How to Create an .Xml File
Open a text editor. To write XML documents you can use a standard text editor such as Notepad. Click on the "Start"...
How to Write a PHP Script Login
With the use of a MySQL database, you can write a script with the PHP: Hypertext Preprocessor, better known as simply PHP...
What Is a PHP File?
A PHP file is a text document containing a specific code that is ran and executed by a program called an interpreter....
How to Create a File Upload Routine in PHP
The ability to upload files on a website is a very useful feature, especially for a site designed for greater user interaction....