idevman / xml-mapper
Used to map XMLs to mapped arrays
Installs: 1 537
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 1
Open Issues: 4
Requires
- illuminate/support: ^6.2|^7.0
Requires (Dev)
- orchestra/testbench: ^4.4
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-11-29 05:27:21 UTC
README
Create a mapped array from XML data using alias from MAP as laravel plugin.
Installation
Dependencies:
- Laravel 6.2+ (v2.0.0)
- Laravel 5.5+ (v1.0.4)
Installation:
- Require the package via Composer
composer require idevman/xml-mapper
Laravel 5.5+
Laravel 5.5+ will autodiscover the package, for older versions add the following service provider
Idevman\XmlMapper\XmlMapperServiceProvider::class,
and alias
'XmlMapper' => 'Idevman\XmlMapper\Support\Facades\XmlMapper',
Usage
To use it lets assume xml content:
<note from="Tove" to="Jani"> <content heading="Reminder" body="Don\'t forget me this weekend!"/> <raw>Simple text</raw> <item number="1" value="50">Table</item> <item number="2" value="40">Chair</item> <item number="3" value="30">Door</item> <item number="4" value="60">Window</item> </note>
So, load this in $xml
variable and call liek this:
$response = XmlMapper::mapTo([ 'from' => '/note@from', 'to' => '/note@to', 'heading' => '/note/content@heading', 'body' => '/note/content@body', 'raw' => '/note/raw', 'simpleItem' => '/note/item', 'simpleItemNumber' => '/note/item@number', 'allItems' => '/note/item[]', 'allItemsNumber' => '/note/item[]@number', ], $xml);
And $response
will contain
[ "from" => "Tove" "to" => "Jani" "heading" => "Reminder" "body" => "Don't forget me this weekend!" "raw" => "Simple text" "simpleItem" => "Table" "simpleItemNumber" => "1" "allItems" => [ 0 => "Table" 1 => "Chair" 2 => "Door" 3 => "Window" ] "allItemsNumber" => [ 0 => "1" 1 => "2" 2 => "3" 3 => "4" ] ]
Grouping attribute nodes
Lets assuma same XML and load this in $xml
variable and call like this:
$response = XmlMapper::mapTo([ 'addressing[from,to]' => '/note[@from,@to]', 'content[header,body]' => '/note/content[@heading,@body]', 'items[sequence,value]' => '/note/item[][@number,@value]', ], $xml);
And $response
will contain this result:
[ "addressing" => [ "from" => "Tove" "to" => "Jani" ] "content" => [ "header" => "Reminder" "body" => "Don't forget me this weekend!" ] "items" => [ [ "sequence" => "1" "value" => "50" ], [ "sequence" => "2" "value" => "40" ], [ "sequence" => "3" "value" => "30" ], [ "sequence" => "4" "value" => "60" ] ] ]
Note that labeling change according key array content