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>

studentxml.xml
<?xml version="1.0"?>
<student>


</student>

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.

Note:

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

student.php
<?php

 //create a new DOM document
 $xmldoc=new DomDocument();
 $xmldoc->formatOutput=true;

//Assign variables to store values

 $id="1";
 $name="Anvita Rai";
 $Rollno="08";
 $schoolname="Guru Nanak english School";
 $age="21";
 
 if($xml=file_get_contents('studentxml.xml')){
 $xmldoc->loadXML($xml);
 $root=$xmldoc->firstChild;
 

  //create a new element
 //first element 'id' is created
 $newElement=$xmldoc->createElement('id');


 //append the created element in first child 'root'
 $root->appendChild($newElement);


 //text is inserted in between the node using createTextNode()
 $idText=$xmldoc->createTextNode($id);


 //append the text in the element
 $newElement->appendChild($idText);
 
 
 //second column of 'name'

 $nameElement=$xmldoc->createElement('name');
 $root->appendChild($nameElement);
 $nameText=$xmldoc->createTextNode($name);
 $nameElement->appendChild($nameText);
 
 //third column 'Rollno'

 $RollnoElement=$xmldoc->createElement('Rollno');
 $root->appendChild($RollnoElement);
 $RollText=$xmldoc->createTextNode($Rollno);
 $RollnoElement->appendChild($RollText);
 
//fourth column 'Schoolname'

 $schoolnameElement=$xmldoc->createElement('schoolname');
 $root->appendChild($schoolnameElement);
 $schoolText=$xmldoc->createTextNode($schoolname);
 $schoolnameElement->appendChild($schoolText);
 
 //fifth column 'age'

 $ageElement=$xmldoc->createElement('age');
 $root->appendChild($ageElement);
 $ageText=$xmldoc->createTextNode($age);
 $ageElement->appendChild($ageText);
 
 
 //save the xml file using save('filename.xml') method
 
 $xmldoc->save('studentxml.xml'); 
 }

?>


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.