Parsing XML file with Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Parsing XML Files Efficiently and Structuring Data with Laravel
Body:
Parsing large and complex XML files can be a daunting task for a developer. You've encountered this issue, and it's essential to convert the data into a structured format to run operations on it later. Here's how you can parse your provided XML using Laravel and create a more efficient solution:
1. Use SimpleXML library in Laravel: Laravel provides built-in support for PHP’s SimpleXML extension. It allows you to work with XML files easily by converting them into native PHP objects. This saves you from dealing with DOMDocument directly. The code below shows how to load the provided XML file as a SimpleXML object using the Laravel Storage facade:
$xml = \Illuminate\Support\Facades\Storage::disk('local')->get('data/XML.xml');
2. Iterate through the XML nodes and extract relevant data: After loading your XML file, you can iterate through its nodes to store your data in a structured format. Here's an example:
$samples = [];
foreach ($xml->community->data1->datatype as $datatype) {
$typeId = (int)$datatype['ID'];
foreach ($datatype->utpricing as $utpricing) {
$baseAmount = (float)$utpricing['BASEAMOUNT'];
$concession = (float)$utpricing['CONCESSION'];
$effectiveAmount = (float)$utpricing['EFFAMOUNT'];
$sampleType = (string)$utpricing->getParentNode()->getName();
// Create a new object containing this information and push it to the samples array.
}
}
3. Convert the SimpleXML object to JSON: To create a more manageable format, you can convert your parsed XML data into JSON using PHP's json_encode function. This will generate an extensible and efficient data structure that allows for easier querying and manipulation later on:
$encodedData = json_encode((array) $xml, JSON_PRESERVE_ZERO_ASCII | JSON_THROW_ON_ERROR);
\Storage::disk('local')->put('parsed_data', $encodedData);
4. Load and manipulate the parsed data: Now that your data is structured in JSON format, you can load it into a Laravel collection using the code below:
$json = Storage::disk('local')->get('parsed_data');
$collection = collect(json_decode($json, true));
You can then query and manipulate your data efficiently. For example, to select all items from data1 with a specific datatype ID, you could run the following query:
$collection->where('data1.datatype_id', $datatypeId)->whereHas('samples', function ($query) use ($datatypeId) {
$query->where('data1type', '001')
->whereHasAny('utpricing', function ($query) use ($concessionType) {
$query->where('contype', $conType);
});
})->get();
This query checks for samples with the data1TYPE equal to your desired datatype ID, while ensuring that they have at least one utpricing node with a specified contype.
Conclusion: Parsing XML files can be time-consuming and complicated if not handled efficiently. By utilizing Laravel's built-in support for SimpleXML and converting your parsed data to JSON format, you can create a structured collection that significantly improves performance when running operations on it later. This approach not only simplifies the processing of large files but also ensures your project is organized and maintainable.