How To Read XML File in Laravel 8 – Example

Share this Article
Reading Time: 3 minutes

Inside this article we will see How to read XML file in laravel 8. There are actually very few simple steps and also you can follow this step in any PHP and it’s framework. This is not laravel specific.

To know the topic, reading xml file in laravel we will consider a sample xml and read it in code.


Above image is view of sample xml file and it’s data. You can download this

Let’s get started.

Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server:

Assuming laravel already installed at system.

Create Controller

We need to create a controller where we will parse or read xml data string from file. Open laravel project into shell or terminal and type this command.

To create Controller –

$ php artisan make:controller ReadXmlController

It will create a file inside /app/Http/Controllers.

Open up the created file ReadXmlController.php into editor


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ReadXmlController extends Controller
    public function index()
        $xmlDataString = file_get_contents(public_path('sample-course.xml'));
        $xmlObject = simplexml_load_string($xmlDataString);
        $json = json_encode($xmlObject);
        $phpDataArray = json_decode($json, true); 
        // echo "<pre>";
        // print_r($phpDataArray);
  • public_path(‘sample-course.xml’) XML file should be placed in public directory. public_path returns the path upto /public folder
  • simplexml_load_string($xmlDataString); It’s a PHP function used to read xml file

Create Route

Open web.php file from /routes

//.. other code

# Import controller
use App\Http\Controllers\ReadXmlController;

Route::get("read-xml", [ReadXmlController::class, "index"]);

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

Open up the URL – http://localhost:8000/read-xml

We hope this article helped you to learn about i.e How To Read XML File in Laravel 8 Tutorial in a very detailed way. To save data From XML file to Database Click here to go.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Learn Web Development Courses @ $4 only.
Web Development Courses @ $4