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.

Output: 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.