Parser Component is a framework agnostic package that provide a simple way to parse XML to array without having to write a complex logic.
Laravel | Parser |
---|---|
4.1.x | 2.1.x |
4.2.x | 2.2.x |
5.0.x | 3.0.x |
5.1.x | 3.1.x |
5.2.x | 3.2.x@dev |
To install through composer, simply put the following in your composer.json
file:
{
"require": {
"orchestra/parser": "~3.0"
}
}
And then run composer install
from the terminal.
Above installation can also be simplify by using the following command:
composer require "orchestra/parser=~3.0"
Next add the service provider in config/app.php
.
'providers' => [
// ...
Orchestra\Parser\XmlServiceProvider::class,
],
Here's a basic example how you can parse XML to simple array:
<api>
<user followers="5">
<id>1</id>
<email>[email protected]</email>
</user>
</api>
$app = new Illuminate\Container\Container;
$document = new Orchestra\Parser\Xml\Document($app);
$reader = new Orchestra\Parser\Xml\Reader($document);
$xml = $reader->load('path/to/above.xml');
$user = $xml->parse([
'id' => ['uses' => 'user.id'],
'email' => ['uses' => 'user.email'],
'followers' => ['uses' => 'user::followers'],
]);