SimpleXML is a PHP extension introduced with PHP 5. It allows users to easily handle XML data in PHP. SimpleXML converts any XML data to an object which can be easily processed with normal property selectors and array iterators.
You must have installed php-simplexml extension on your system to use examples of this tutorial.
A Sample XML File
Here is a sample XML file used for this tutorial. The XML filename is employees.xml which you will see in further examples of this tutorial.
Read Specific XML Elements
Use simplexml_load_file function to load external XML file in your PHP program and create an object. After that, you can access any element from the XML by this object as follows.
If the XML file is available on the remote server, you can use HTTP URL for the XML file as followings:
Read XML Elements In A Loop
In this example, we use the foreach method to iterate through the entire XML file and read elements from XML. Foreach loop access all children of an object.
Tom, Cruise, MD, 500000 Tyler, Horne, CEO, 250000