Read xml file using php

0
161

In this post we will see how to read an xml file with the help of php function.

read xml file using php

most of the time we need to fetch records from xml file, so we use a php function
The simplexml_load_file() function which helps to converts the specified XML file into
a SimpleXMLElement object.
Syntax

simplexml_load_file(file,classname,options,ns,is_prefix);

here we have xml file which is named as sitemap.xml, we fetch  all dates from sitemap using simplexml_load_file() function.

fetch.php
in fetch.php file we fetch all records from sitemap.xml file.

<?php
$xml = simple_load_file('sitemap.xml');
foreach($xml->children() as $data)
{
echo $data->to."<br>";
echo $data->from."<br>";
echo $data->heading."<br>";
echo $data->body."<br>";
}
?>