In this tutorial you will learn about the Laravel 8 Generate and Read XML File Tutorial Example and its application with practical example.
In this Laravel 8 Generate and Read XML File Tutorial Example tutorial I will show you how to read xml file in laravel 8 application. In this tutorial you will learn to generate and read xml file in laravel 8 application. In this article I will share example to generate and read xml file in laravel 8 application.
Laravel 8 Read XML File Tutorial Example
In this step by step tutorial I will demonstrate you how to read xml file in laravel. Please follow the instruction given below:
Create Sample XML File
Please create a sample xml file with below code and save it as name “sample-course.xml” and save it in public directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <online_course> <course> <name>WordPress Theme Development</name> <price>Free</price> <category>WordPress</category> <videos>35</videos> </course> <course> <name>WordPress Plugin Development</name> <price>Free</price> <category>WordPress</category> <videos>50</videos> </course> </online_course> |
Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel lara8blog |
Make sure you have composer installed.
Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=lara8blog DB_USERNAME=root DB_PASSWORD= |
Create Controller
In this step we will create a controller to parse or read xml data from file using following command:
1 |
php artisan make:controller ReadXmlController |
Once the above command executed, it will create a controller file ReadXmlController.php in app/Http/Controllers/ directory. Open the ReadXmlController.php file and put the following code in it.
app/Http/Controllers/ReadXmlController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php 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); dd($phpDataArray); } } |
Create Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 |
# Import controller use App\Http\Controllers\ReadXmlController; Route::get("read-xml", [ReadXmlController::class, "index"]); |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/read-xml |