BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
PHP XML
PHP XML RPC Function Server Call method
PHP XML RPC Parse Method Descriptions
PHP XML RPC Function ServerCreate
PHP XML RPC Function GetType
PHP XML RPC Function EncodeRequest
PHP XML RPC Function Encode
PHP XML RPC Function DecodeRequest
PHP DOMXPath Function registerNamespace
PHP DOMXPath Function query
PHP DOMText Function splitText
PHP DOMText Function isWhitespaceInElementContent
PHP DOMNode Function replaceChild
PHP DOMNode Function lookupPrefix
PHP DOMNode Function lookupNamespaceURI
PHP DOMNode Function isSupported
PHP DOMNode Function isSameNode
PHP DOMNode Function insertBefore
PHP DOMNode Function hasChildNodes
PHP DOMNode Function hasAttributes
PHP DOMNode Function cloneNode
 
 

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :2817

 

PHP SimpleXML: Create an XML Document

PHP Version: PHP3, PHP4, PHP5

Description

In This tutorial, I will be covering how to create an well-formed XML document from a HTML form. In this example, I have created a html page to enter employee details and the data entered by user is stored in an xml file.

Example of Html Page

UI of html page I have created as below.

Example of Html script

The html script got the above page is as below.

<html>

<head>

<title>Employee Data</title>

</head>

<body>

<p>Employee Data</p>

<form method="POST" action="php/employee.php">

<input type="hidden" name="create_employee" value="true">

<table>          

  <tr>

    <td width="27%">Employee Name:</td>

    <td width="73%"><input type="text" name="empName" size="20"></td>

  </tr>

  <tr>

    <td width="27%">Employee Address</td>

    <td width="73%"><input type="text" name="empAddress" size="73"></td>

  </tr>

  <tr>

    <td width="27%">Employee SSN</td>

    <td width="73%"><input type="text" name="empSSN" size="20"></td>

  </tr>

  <tr>

    <td width="27%">Company Name</td>

    <td width="73%"><input type="text" name="empCompany" size="73"></td>

  </tr>

  <tr>

    <td width="27%">XML File Name</td>

    <td width="73%"><input type="text" name="xmlfileName" size="20"></td>

  </tr>

</table>

  <p align="center">

  <input type="submit" value="Submit" name="B1"></p>

</form>

</body>

</html>

Example of PHP usage to create xml file

In the above employee form, on submit the data will be posted on php/employee.php file. In employee.php file, I have gathered all POSTED data as below:

if(isset($_POST['create_employee'])){

        echo "Employee Data Posted";

        $xmlfileName = $_POST['xmlfileName'];

        $empName = $_POST['empName'];

        $empAddress = $_POST['empAddress'];

        $empSSN = $_POST['empSSN'];

        $empCompany = $_POST['empCompany'];

        $xml_dec = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

After gathering all data from the form then, I created a variable to hold all the data entered by user in string format.

        $rootELementStart = "<employee>";

        $rootElementEnd = "</employee>";

        $xml_doc=  $xml_dec;

        $xml_doc .=  $rootELementStart;

        $xml_doc .=  "<employeename>";

        $xml_doc .=  $empName;

        $xml_doc .=  "</employeename>";

        $xml_doc .=  "<employeeaddress>";

        $xml_doc .=  $empAddress;

        $xml_doc .=  "</employeeaddress>";

        $xml_doc .=  "<SSN>";

        $xml_doc .=  $empSSN;

        $xml_doc .=  "</SSN>";

        $xml_doc .=  "<company>";

        $xml_doc .=  $empCompany;

        $xml_doc .=  "</company>";

        $xml_doc .=  $rootElementEnd;

        $default_dir = "xml_files/";

        $default_dir .=   $xmlfileName .".xml";

Here I have taken default directory as xml_file directory.

After creating string representation of employee data, I have used following PHP script to store in the file

$fp = fopen($default_dir,'w');

            $write = fwrite($fp,$xml_doc);

fopen function opens file or URL. I assume that xml_file directory is there so I have checked for error during fopen function call.

I have used fwrite($fp,$xml_doc); to store the xml string into a file.

Output

<?xml version="1.0" encoding="UTF-8"?>

<employee>

            <employeename>Markwilson</employeename>

            <employeeaddress>Topxml</employeeaddress>

            <SSN>12345</SSN>

            <company>TopXML</company>

</employee>


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
help desk support
swimming pool contractor
halloween masks
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP