How to Read XML File in Laravel 6,7,8

Let’s have a look at an example of how to read an xml file in Laravel. I’ll show you an example of laravel reading an xml file. We’ll discuss about how to read an xml file in Laravel. In this tutorial, I’ll show you how to read an xml file in Laravel.

In Laravel 6, Laravel 7, and Laravel 8, you can easily read xml files.

Also Read: Laravel image validation example
XML Official DOC: Extensible Markup Language (XML)

Create Controller

Now let’s create XMLReaderController:

php artisan make:controller XMLReaderController 

App\Http\Controllers

<?php 
   
namespace App\Http\Controllers; 
    
use Illuminate\Http\Request; 
   
class XMLReaderController extends Controller 
{ 
    /** 
     * Write code on Method 
     * 
     * @return response() 
     */ 
    public function index() 
    { 
        $xmlString = file_get_contents(public_path('put-the-xml-file-path')); 
        $xmlObject = simplexml_load_string($xmlString); 
                    
        $json = json_encode($xmlObject); 
        $phpArray = json_decode($json, true);  
    
        dd($phpArray); 
    } 
}

Hope it helps!