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
$root->appendChild($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
Status
Address
Age
Email Address
Comment

1. Example code for create XML document using PHP.

<?php
$dom = new DOMDocument();
$dom->encoding = 'utf-8';
$dom->xmlVersion = '1.0';
$dom->formatOutput = true;
$dom->xmlStandalone = true;
$xml_file_name='test.xml';
$root = $dom->createElement('Users');

$user_node=$dom->createElement('User');
$attr_user_id = new DOMAttr('user_id', '1020');
$user_node->setAttributeNode($attr_user_id);

$child_node_first_name=$dom->createElement('FirstName','Duminda');
$user_node->appendChild($child_node_first_name);
$child_node_last_name=$dom->createElement('LastName','Chamara');
$user_node->appendChild($child_node_last_name);

$child_node_email_address=$dom->createElement('EmailAddress','abc@gmail.com');
$user_node->appendChild($child_node_email_address);

$child_node_address=$dom->createElement('Address');
$cdata_node = $dom->createCDATASection('Duminda Chamara,
80062,
Galle,
SriLanka.');
$child_node_address->appendChild($cdata_node);
$user_node->appendChild($child_node_address);

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

$child_node_age=$dom->createElement('Age','12');
$attr = new DOMAttr('married', 'false');
$child_node_age->setAttributeNode($attr);
$user_node->appendChild($child_node_age);

$root->appendChild($user_node);
$dom->appendChild($root);
$dom->save($xml_file_name);
?>
Output
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--this is a test comment add your custom text-->
<Users>
<User user_id="1020">
<FirstName>Duminda</FirstName>
<LastName>Chamara</LastName>
<EmailAddress>abc@gmail.com</EmailAddress>
<Address><![CDATA[H.H.D.Chamara,
80062,
Galle,
SriLanka.]]></Address>
<Age married="false">12</Age>
</User>
</Users>

©-Copyright By Duminda Chamara