DEV Community

devcse
devcse

Posted on • Originally published at codesnipeet.com

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.
Create Controller

Now let’s create XMLReaderController:

php artisan make:controller XMLReaderController 
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Hope it helps!

Top comments (0)