How to Read XML File in Laravel?

Admin   Laravel   251  2021-03-14 10:30:05

Now let's see example of how to read xml file in laravel. I will let you know example of laravel read xml file example. We will talk about laravel read xml file. I will learn you how to read xml file in 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:

 

<?php

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);

dd($phpArray);

}

}

It will help you....