Skip to content

mahmudkuet11/array-to-xml

 
 

Repository files navigation

Convert an array to xml

Latest Version Software License Build Status Quality Score StyleCI Total Downloads

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

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciated you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

All postcards are published on our website.

Install

You can install this package via composer.

composer require spatie/array-to-xml

Usage

use Spatie\ArrayToXml\ArrayToXml;
...
$array = [
    'Good guy' => [
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];

$result = ArrayToXml::convert($array);

After running this piece of code $result will contain:

<?xml version="1.0"?>
<root>
    <Good_guy>
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </Good_guy>
    <Bad_guy>
        <name>Sauron</name>
        <weapon>Evil Eye</weapon>
    </Bad_guy>
</root>

Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify this argument (or set it to an empty string) "root" will be used.

$result = ArrayToXml::convert($array, 'customrootname');

By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of this behaviour you can set the third argument to false. We'll leave all keynames alone.

$result = ArrayToXml::convert($array, 'customrootname', false);

You can use a key named _attributes to add attributes to a node.

$array = [
    'Good guy' => [
        '_attributes' => ['attr1' => 'value']
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];

$result = ArrayToXml::convert($array);

This code will result in:

<?xml version="1.0"?>
<root>
    <Good_guy attr1="value">
        <name>Luke Skywalker</name>
        <weapon>Lightsaber</weapon>
    </Good_guy>
    <Bad_guy>
        <name>Sauron</name>
        <weapon>Evil Eye</weapon>
    </Bad_guy>
</root>

If your input contains something that cannot be parsed a DOMException will be thrown.

To add attributes to the root element provide an array with an _attributes key as the second argument. The root element name can then be set using the rootElementName key.

$result = ArrayToXml::convert($array, [
    'rootElementName' => 'helloyouluckpeople',
    '_attributes' => [
        'xmlns' => 'https://github.com/spatie/array-to-xml',
    ],
]);

Testing

vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Credits

License

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

About

A simple class to convert an array to xml

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%