Skip to content

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

License

Notifications You must be signed in to change notification settings

vaclavvanik/xml-to-array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Convert xml to array

This package provides a very simple class to convert xml to array.

Install

You can install this package via composer.

composer require vaclavvanik/xml-to-array

Usage

use VaclavVanik\XmlToArray\XmlToArray;

$xml = <<<'XML'
<root>
    <good_guy>
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </good_guy>
    <good_guy>
        <name><![CDATA[<h1>Gandalf</h1>]]></name>
        <weapon>Staff</weapon>
    </good_guy>
    <bad_guy lang="Black Speech">
        <name>Sauron</name>
        <weapon>Evil Eye</weapon>
    </bad_guy>
</root>
XML;

$result = XmlToArray::stringToArray($xml);

After running this piece of code $result will contain:

[
    'root' => [
        'good_guy' => [
            [
                'name' => 'Luke Skywalker',
                'weapon' => 'Lightsaber',
            ],
            [
                'name' => '<h1>Gandalf</h1>',
                'weapon' => 'Staff',
            ],
        ],
        'bad_guy' => [
            '@attributes' => [
                'lang' => 'Black Speech',
            ],
            'name' => 'Sauron',
            'weapon' => 'Evil Eye',
        ],
    ],
];

Converting XML file is also available:

use VaclavVanik\XmlToArray\XmlToArray;

$result = XmlToArray::stringToArray('my.xml');

Converting DOMDocument directly:

use DOMDocument;
use VaclavVanik\XmlToArray\XmlToArray;

$doc = new DOMDocument();
//$doc->loadXML(...);

$xmlToArray = new XmlToArray($doc);
$result = $xmlToArray->toArray();

Run check - coding standards and php-unit

make check

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published