Site Loader
Get a Quote

Insert data into XML file


insert data into XML fileHi readers today we come up with the new concept of PHP with XML(eXtensible Markup Language). Here in this tutorial, we begin with a brief introduction of XML then proceed to the simple example to insert data into XML using PHP code with an explanation of full source code which is in a readable format and then the snapshot view which makes you more clear. So no need to worry even for beginners!.Here we starts topic on insert data into XML file.

Brief Introduction On XML

XML stands for eXtensible Markup Language which is a Markup language similar to HTML but not exactly like that.

HTML is mainly designed to give layout structure of any web page which mainly focuses on how data looks but XML is designed mainly to transport data and store it independent of any hardware and software tool which focuses on what data is.

There is two main differences between XML and HTML:

1.)  In XML there is no specific tags defined which can be used but we can create our own tags as we like whereas in HTML there is a specific tags      defined already which is used to make the layout of website like <p>,<h>,<img> etc. and other than those defined tags we can’t create our own tags.

2.)  XML is an strict markup language that means every opened tags must have a closed tags whereas in HTML there is no such restrictions.

Simple Example to insert data into XML file: 

Here is the Simple example to insert data into XML file via PHP script.

Step 1: First Create a XML file.

Here is the simple XML file named studentxml.xml with only one element <student>..</student>

<?xml version="1.0"?>


Step 1: Create a PHP file  ‘student.php’ to insert data into XML file.

Here in this step we create a PHP file named ‘student.php’ to insert records using XML. To insert records here we use a tree like structure.

First of all we create the new DOM element using new keyword and then store all the data in the variable. If the created .xml file has some contents then starts inserting the element after loading the .xml file and capture the first child(root node) in a $root variable.

The element is created under root element using createElement() method and then append to the root element using appendChild() method and insert the text in between the created element using createTextNode() method.This process repeats for each element you want to insert.


DOM (Document Object Model) is an object so we use the access operator(->) to access different methods like loadXML()createElement(), createTextNode(), appendChild() method.


 //create a new DOM document
 $xmldoc=new DomDocument();

//Assign variables to store values

 $name="Anvita Rai";
 $schoolname="Guru Nanak english School";

  //create a new element
 //first element 'id' is created

 //append the created element in first child 'root'

 //text is inserted in between the node using createTextNode()

 //append the text in the element
 //second column of 'name'

 //third column 'Rollno'

//fourth column 'Schoolname'

 //fifth column 'age'

 //save the xml file using save('filename.xml') method


Screenshot View:

After running the student.php file on browser. The following dialog box appears to reload the file. Press ‘yes to reload a file after inserting data into XML file through PHP Script.

insert data into XML file
insert data into XML file
The screenshot view of student.xml file after clicking Yes button



insert data into XML file
insert data into XML file

I hope you would like this tutorial to insert data into XML file using PHP Script!


Next Topic to read data from XML

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.