Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Read XML file Using PHP

Friday, Dec 15, 2017

Tags:- PHP


Extensible Markup Language (XML) is a simple, very flexible text format it is also playing an increasingly important role in the exchange of a wide variety of data on the Web and elsewhere.In this tutorial we will show you how to read XML file using PHP.You may also like read and write csv file using PHP.


Read XML File Using PHP


To Read XML File It Takes Only One Step:-

  1. Make a PHP file to read xml


Step 1.Make a PHP file to read xml

We make a PHP file and save it with a name read_xml.php

<html>
<body>
<div id="wrapper">

<?php
$xml = <<<XML
 <employee>
  <employee name="Amar" age="29" salary="25000" />
  <employee name="Rahul" age="22" salary="18000" />
  <employee name="Aarti" age="31" salary="40000" />
  <employee name="Pooja" age="27" salary="27000" />
  <employee name="Manoj" age="34" salary="31000" />
  <employee name="Shivam" age="24" salary="20000" />
  <employee name="Preeti" age="30" salary="18000" />
 </employee>
XML;

$employees = new SimpleXMLElement($xml);
foreach($employees as $employee)
{
 echo $employees['name'], "\n";
 echo $employees['age'], "\n";
 echo $employees['salary'], "\n";
 echo "<br><br>";
}
?>

</div>
</body>
</html>

In this step we write some sample XML data related to employee details like name, age and salary and after that we use SimpleXMLElement function to read xml file and create an object of that to read XML variable and display them using for each loop.You may also like create xml sitemap using PHP.


That's all, this is how to import excel file data into mysql database using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.