Duminda Chamara

Sunday, June 13, 2010

How to create xml file in php

How to create XML file in PHP?

Following example shows how to Create a XML file in PHP.

XML (Extensible Markup Language) file describes structured data in plain text based on tags under the standards of W3C. XML files make the data highly portable. So it can be use to transfer data between wide variety of applications. As an example you can make a connection between PHP and Flash using a xml file easily. XML is very useful to transfer data over the Internet. You can define any tag in a xml file. There should be starting tag and corresponding closing tag.

In PHP The DOM extension allows you to operate on XML documents through the DOM API. The following example describes how to create a simple xml file using DOM extensions. Here you can create a xml file in very few steps.

$dom = new DOMDocument(); // Represents an entire XML document (the root of the document tree)

After initializing the xml document you can set the document encoding and the xml version as follows.

$dom->encoding = 'utf-8';//set the document encoding
$dom->xmlVersion = '1.0';//set xml version

You can create the xml document with nice indentation. This is important when you reading the document.

$dom->formatOutput = true;//Nicely formats output with indentation and extra space
$dom->xmlStandalone = true; //Whether or not the document is standalone

Creating and adding the nodes to the XML file

$root = $dom->createElement('Root');//Creates the root element of the xml file
$node=$dom->createElement('Child_1');//Create a child node
$child_node_1=$dom->createElement('FirstName','Duminda');//Create child element
$node->appendChild($child_node_1);//append child element to the node
$dom->appendChild($root);//append the root node
$dom->save('test.xml');//save the xml file

Adding attributes to the nodes of the XML file

$node=$dom->createElement('User');//create an element
$attr_user_id = new DOMAttr('user_id', '1020'); //Create an attribute
$node->setAttributeNode($attr_user_id);//set created attribute to the node

Adding CDATA to the XML file

$node=$dom->createElement('Address');//create a child node
$cdata_node = $dom->createCDATASection('type your address here ');//create a CDATA Section
$node->appendChild($cdata_node); //append CDATA Section to the node

Adding comments to the XML file

$comment = $dom->createComment('this is a test comment');//create a comment
$comment->appendData('add your custom text');//add more comment
$dom->appendChild($comment);//Add some comment to the xml file

Demo Create XML file in PHP
First Name
Last Name
Email Address

1. Example code for create XML document using PHP.

$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$dom->xmlStandalone = true;
$root = $dom->createElement('Users');

$attr_user_id = new DOMAttr('user_id', '1020');



$cdata_node = $dom->createCDATASection('Duminda Chamara,

$comment = $dom->createComment('this is a test comment');
$comment->appendData('add your custom text');

$attr = new DOMAttr('married', 'false');

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--this is a test comment add your custom text-->
<User user_id="1020">
<Age married="false">12</Age>

©-Copyright By Duminda Chamara