All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Read XML file Using PHP

Last Updated : Jul 1, 2023

IN - PHP | Written & Updated By - Amruta

In this tutorial we will show you how to read XML file using 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.

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 read xml file using php. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on read xml file using php helps you and the steps and method mentioned above are easy to follow and implement.