How to read XML file in laravel

In this example, we will load an XML file and convert it into an array in the Laravel application. Before we start let’s know more about XML.

What is XML?

XML stands for Extensible Markup Language. It’s a markup language like HTML to store and transmit data. It has per-defined rules for encoding documents in format. It’s a text-based format to store structured data into files. We can store data like book information, student data, documents, or configurations in it.

XML is popular because it stores data in text format so there are no hardware or software dependencies to read or alter data.

For this example, we will use the simplexml_load_string() function to create an XML object from the file. It will work on all versions of Laravel like Laravel 6, Laravel 7, Laravel 8, and Laravel 9.

Create XML file

First of all, let’s create an XML file in our file system or you can use any existing XML file for this example. We will create an XML file

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <row>
    <id>001</id>
    <name>Alex</name>
    <email>alex@gmail.com</email>
  </row>
  <row>
    <id>002</id>
    <name>Maria</name>
    <email>maria.332@gmail.com</email>
  </row>
  <row>
    <id>003</id>
    <name>vivian</name>
    <email>vivian.rechards@hotmail.com</email>
  </row>
  <row>
    <id>004</id>
    <name>ceasy</name>
    <email>ceasy@gmail.com</email>
  </row>
</root>

If you don’t have any XML file then copy the above code and paste it into text-editor and save it as employees.xml at a public directory.

Read XML File Content

In Laravel, you can read XML file data into any class like Controller, Model, Seeder, etc. Here, we will use a controller to read XML data. So open any controller from your application and place the below code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class XmlController extends Controller
{
    public function readXml(){
        $xmlContent = file_get_contents(public_path('employee.xml'));
        $xmlObject = simplexml_load_string($xmlContent);

        $jsonData = json_encode($xmlObject);
        $dataArray = json_decode($jsonData, true);

        dd($dataArray);
    }
}

This function will use the file_get_contents() function to read a file as a normal file and get data from that file to the object. Then we will create an XML object from that file content and lastly, we will encode and decode XML data as JSON so it will return array data.

Here, we just print data into the browser screen. But you can use it as per your requirements like storing it in a database performing some calculations on that data or displaying it as a table on a webpage. So it’s up to you, How you are gonna use it.

Below code snippet is shows the output of XML data using the dd() function.

array:1 [
  "row" => array:4 [
    0 => array:3 [
      "id" => "001"
      "name" => "Alex"
      "email" => "alex@gmail.com"
    ]
    1 => array:3 [
      "id" => "002"
      "name" => "Maria"
      "email" => "maria.332@gmail.com"
    ]
    2 => array:3 [
      "id" => "003"
      "name" => "vivian"
      "email" => "vivian.rechards@hotmail.com"
    ]
    3 => array:3 [
      "id" => "004"
      "name" => "ceasy"
      "email" => "ceasy@gmail.com"
    ]
  ]
]

Conclusion

In this example, we have created an XML file and read that file into Laravel using in-built PHP functions. It will work on core PHP too but you need to modify some things like giving a full path while reading. Still, if you have any queries feel free to place a comment below.