How to Read XML File in Laravel?

By Hardik Savani | January 15, 2021 | Category : Laravel


Today, i will let you know example of how to read xml file in laravel. i explained simply about laravel read xml file. you will learn laravel read xml example. We will use read xml file in laravel. Let's see bellow example convert xml file to php array laravel.

You can easily read xml file in laravel 6, laravel 7 and laravel 8 version.

In this example, i will give you very simple example of convert xml to php array in laravel. so here, we will keep one simple example file and convert into php array.

Let's see now:

Add Simple Xml File:

you can create or download simple xml file as like bellow:

Download XML Demo File

Controller Code:

now you can see how convert xml to array using bellow code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class XMLController extends Controller



* Write code on Method


* @return response()


public function index()


$xmlString = file_get_contents(public_path('sample.xml'));

$xmlObject = simplexml_load_string($xmlString);

$json = json_encode($xmlObject);

$phpArray = json_decode($json, true);




you will see bellow output:




[food] => Array


[0] => Array


[name] => Belgian Waffles

[price] => $5.95

[description] => Two of our famous Belgian Waffles with plenty of real maple syrup

[calories] => 650


[1] => Array


[name] => Strawberry Belgian Waffles

[price] => $7.95

[description] => Light Belgian waffles covered with strawberries and whipped cream

[calories] => 900


[2] => Array


[name] => Berry-Berry Belgian Waffles

[price] => $8.95

[description] => Light Belgian waffles covered with an assortment of fresh berries and whipped cream

[calories] => 900


[3] => Array


[name] => French Toast

[price] => $4.50

[description] => Thick slices made from our homemade sourdough bread

[calories] => 600


[4] => Array


[name] => Homestyle Breakfast

[price] => $6.95

[description] => Two eggs, bacon or sausage, toast, and our ever-popular hash browns

[calories] => 950




you can check with this code.

i hope it can help you...

Tags : Laravel

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: