Site Loader
Get a Quote

read or display data from XML file


Read or display data from the XML fileHi readers, as we have already explained how we can insert data into XML file in our previous post today we come up with the topic of read or display data from XML file. Here in this tutorial, we begin with a brief introduction of XML then proceed to the simple example to read/select/display 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 start topic on read or display data from the 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 per need 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 read or  display data from the XML file: 

Here is the Simple example to select or display data from XML file via a PHP script.

Step 1: First Create an XML file or use a saved XML file.

Here we have used the simple saved XML file named studentxml.xml

studentxml.xml
<?xml version="1.0"?>
<student>
<id>1</id>
<name>Anvita Rai</name>
<Rollno>08</Rollno>
<schoolname>Guru Nanak english School</schoolname>
<age>21</age>

</student>

Step 2: Create a PHP file  ‘readxml.php’ to display or read data from XML file.

Here in this step, we can use either of the two methods described below i.e by Specific id or directly by attribute name. One thing is common in both the files that here we use the SimpleXML parser and simplexml_load_file() method to load the .xml files which are to be read.

(1)   Create a PHP file named ‘readxml.php’ to select data by specific id in XML. Load the file then provide the path using xpath and store it in a variable.If it is not empty the print all the records one by one else show ” no records are there” using echo statement

Note:     DOM (Document Object Model) is an object so we use the access operator(->) to access different attribute.

readxml.php
<?php
/* PHP Script to read an xml file and display data by particular "id"*/

/* simplexml parser is used to load the xml file if exists
 otherwise show error message and exit using die() function 
 */

$xml=simplexml_load_file("studentxml.xml") or die("Error: cannot create Object");

$student=$xml->xpath('//student[id=1]');

//print all the records of loaded xml file in an associative array in format of key & value pair
print_r($xml);

//print all the records one by one using echo and print statement

if(!empty($student)){
 $item=$student[0];
 echo "<br><br>All the records one by one:<br>id:=";
 print $item->id;
 echo "<br>Name:= ";
 print $item->name;
 echo "<br>Rollno:= ";
 print $item->Rollno;
 echo "<br>School Name:=";
 print $item->schoolname;
 echo "<br>Age:=";
 print $item->age;
 echo "<br>";
}
else
{ echo "No records";
}

?>

 OR

(2.)  Create a PHP file named read1.xml and copy the below code and then save it.

Here first Load the .xml file then use the print_r() method to print all the records in an associative array of key-value pair to get the attribute name i.e. Key so that it is further used to display data by using access operator(->).

 read1.xml
<?php

/*--- php code begins---*/
/* ---PHP Script to read an xml file and display data by "attribute" 

   --- simplexml parser is used to load the xml file if exists
       otherwise show error message and exit using die() function 
 ---*/

$xml=simplexml_load_file("studentxml.xml") or die("Error: cannot create Object");

//print all the records of loaded xml file in an associative array in format of key & value pair
print_r($xml);

//print records directly by attribute "name" using echo statement

echo "<br><br>$xml->name";

/*--php code ends--*/
?>

Screenshot View of readxml.xml file:

Read or display data from the XML file
Read or display data from the XML file

Screenshot View read1.xml file:

Read or display data from the XML file
Read or display data from the XML file

Download source code

 display-data-from-XML.zip

 

I hope you would like this tutorial to read or display data from XML file using PHP Script!

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.