How to Read XML file Using PHP
XML is the acronym for Extensible Markup Language. SimpleXMLElement is an extension that allows us to easily manipulate and get XML data. SimpleXMLElement turns an XML document into a data structure you can iterate through like a collection of arrays and objects. You may also like How to Create Dynamic XML Sitemap in PHP and How to create RSS Feed reader using PHP.
PHP Code
<?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>
XML;
$employees = new SimpleXMLElement($xml);
echo "<pre>";
print_r($employees);
?>
Output
SimpleXMLElement Object
(
[employee] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Amar
[age] => 29
[salary] => 25000
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Rahul
[age] => 22
[salary] => 18000
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => Aarti
[age] => 31
[salary] => 40000
)
)
)
)
Leave a Reply