How to Read XML File in Laravel

Sovary October 22, 2022 587
3 minutes read

Today I will explain and give example how to read xml file in Laravel, we will let you know how to convert xml format into php array. We will talk about how to read XML file, so you will learn how to turn xml file in array php. This example you will can apply easily read XML file in Laravel 5, Laravel 6, Laravel 7, Laravel 8 and Laravel 9 version.

So let's take a look below example how to read xml file into php array.

Convert XML File to Array PHP

  • Step 1 - Install Laravel
  • Step 2 - Create Controller
  • Step 3 - Add Route
  • Step 4 - Run Laravel Server

Step 1 - Install Laravel

Firstly, we are going to install Laravel project, so you need internet connection to dowload Laravel  app with command below

composer create-project laravel/laravel blog

Step 2 - Create Controller

We will create a controller file to implement method to reading the XML file and convert to PHP Array. Let's run command below to generated controller file.

php artisan make:controller XMLController

We will read file XML in public directory which has file feed.xml or you can download XML file xml file

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
</catalog>

Open file app -> Http -> Controllers -> XMLController.blade.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class XMLController extends Controller
{

    public function index()
    {
        $xml = file_get_contents(public_path('feed.xml'));
        $xmlObject = simplexml_load_string($xml);
                   
        $json = json_encode($xmlObject);
        $phpArray = json_decode($json, true); 
        dd($phpArray);
    }
}

Step 3 - Add Route

Now we will add route link, so that we can access link to call function in controller file.

Open file routes -> web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\XMLController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/index', [XMLController::class, 'index']);

Step 4 - Run Laravel Server

We just want to know if we can convert XML to PHP Array, so we no need create and display in blade file. Following command to start server

php artisan serve

After server run then open your browser and hit the following url to see the web page http://localhost:8000/

In browser you will see on screen.

xml file demo read to arrayThanks for read this article, hope it would help your to learn how to convert XML to PHP array. Have a nice day!

You might also like...

 

Laravel  PHP  Laravel 9 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him