Best laravel framework open-source packages.

Xml to array

Incredibly simple XML to array PHP converter
Updated 4 months ago

XML to array PHP converter

This package provides a very simple class to convert XML string to array.

Why should I use this package?

  • because you don't like XML
  • because other packages generate inconsistent array structure
  • because you can't serialize SimpleXML objects (but can serialize arrays)
  • because you just want to use arrays

Install

You can install this package via composer.

composer require clearstream/xml-to-array

Usage

use Clearstream\XmlToArray\XmlToArray;

$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<response success="true">
    <users>
        <user id="1" name="Illia" />
        <user id="2" name="Trevor" />
    </users>
    <settings>
        <mode value="light" />
        <color value="purple" />
    </settings>
    <article id="1">
        Hello World!
    </article>
    <empty></empty>
    <cdata><![CDATA[<sender>John Doe</sender>]]></cdata>
</response>
XML;

$array = XmlToArray::convert($xml);

After running this piece of code $array will contain:

[
    'response' => [
        '@success' => 'true',
        '#text' => '',
        'users' => [
            [
                '#text' => '',
                'user' => [
                    [
                        '@id' => '1',
                        '@name' => 'Illia',
                        '#text' => '',
                    ],
                    [
                        '@id' => '2',
                        '@name' => 'Trevor',
                        '#text' => '',
                    ],
                ],
            ],
        ],
        'settings' => [
            [
                '#text' => '',
                'mode' => [
                    [
                        '@value' => 'light',
                        '#text' => '',
                    ],
                ],
                'color' => [
                    [
                        '@value' => 'purple',
                        '#text' => '',
                    ],
                ],
            ],
        ],
        'article' => [
            [
                '@id' => '1',
                '#text' => 'Hello World!',
            ],
        ],
        'empty' => [
            ['#text' => ''],
        ],
        'cdata' => [
            ['#text' => '<sender>John Doe</sender>'],
        ],
    ],
]

If your input contains something that cannot be parsed a \Clearstream\XmlToArray\XmlToArrayException will be thrown.

Testing

vendor/bin/phpunit

License

GNU GENERAL PUBLIC LICENSE. Please see License File for more information.

Tags xml php