2017 © Pedro Peláez
 

library array-to-xml

Convert an array to xml

image

spatie/array-to-xml

Convert an array to xml

  • Friday, May 4, 2018
  • by Spatie
  • Repository
  • 10 Watchers
  • 276 Stars
  • 206,145 Installations
  • PHP
  • 48 Dependents
  • 1 Suggesters
  • 70 Forks
  • 4 Open issues
  • 20 Versions
  • 28 % Grown

The README.md

Convert an array to xml

Latest Version Software License Tests Total Downloads, (*1)

This package provides a very simple class to convert an array to an xml string., (*2)

Support us

, (*3)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*4)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*5)

Install

You can install this package via composer., (*6)

``` bash composer require spatie/array-to-xml, (*7)


## Usage ```php 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:, (*8)

<?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>

Setting the name of the root element

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., (*9)

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

Handling key names

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., (*10)

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

Adding attributes

You can use a key named _attributes to add attributes to a node, and _value to specify the value., (*11)

$array = [
    'Good guy' => [
        '_attributes' => ['attr1' => 'value'],
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ],
    'The survivor' => [
        '_attributes' => ['house'=>'Hogwarts'],
        '_value' => 'Harry Potter'
    ]
];

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

This code will result in:, (*12)

<?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>
    <The_survivor house="Hogwarts">
        Harry Potter
    </The_survivor>
</root>

Note, that the value of the _value field must be a string. (More), (*13)

Using reserved characters

It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters., (*14)

$array = [
    'Good guy' => [
        'name' => [
            '_cdata' => '

Luke Skywalker

' ], 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => '

Sauron

', 'weapon' => 'Evil Eye' ] ]; $result = ArrayToXml::convert($array);

This code will result in:, (*15)

<?xml version="1.0"?>
<root>
    <Good_guy>
        <name><![CDATA[<h1>Luke Skywalker</h1>]]></name>
        <weapon>Lightsaber</weapon>
    </Good_guy>
    <Bad_guy>
        <name>&lt;h1&gt;Sauron&lt;/h1&gt;</name>
        <weapon>Evil Eye</weapon>
    </Bad_guy>
</root>

If your input contains something that cannot be parsed a DOMException will be thrown., (*16)

Customize the XML declaration

You could specify specific values in for: - encoding as the fourth argument (string) - version as the fifth argument (string) - DOM properties as the sixth argument (array) - standalone as seventh argument (boolean), (*17)

$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], true);

This will result in:, (*18)

<?xml version="1.1" encoding="UTF-8" standalone="yes"?>

Adding attributes to the root element

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., (*19)

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

Using a multi-dimensional array

Use a multi-dimensional array to create a collection of elements., (*20)

$array = [
    'Good guys' => [
        'Guy' => [
            ['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
            ['name' => 'Captain America', 'weapon' => 'Shield'],
        ],
    ],
    'Bad guys' => [
        'Guy' => [
            ['name' => 'Sauron', 'weapon' => 'Evil Eye'],
            ['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
        ],
    ],
];

This will result in:, (*21)

<?xml version="1.0" encoding="UTF-8"?>
<helloyouluckypeople xmlns="https://github.com/spatie/array-to-xml">
    <Good_guys>
        <Guy>
            <name>Luke Skywalker</name>
            <weapon>Lightsaber</weapon>
        </Guy>
        <Guy>
            <name>Captain America</name>
            <weapon>Shield</weapon>
        </Guy>
    </Good_guys>
    <Bad_guys>
        <Guy>
            <name>Sauron</name>
            <weapon>Evil Eye</weapon>
        </Guy>
        <Guy>
            <name>Darth Vader</name>
            <weapon>Lightsaber</weapon>
        </Guy>
    </Bad_guys>
</helloyouluckypeople>

Using Closure values

The package can use Closure values:, (*22)

$users = [
    [
        'name' => 'one',
        'age' => 10,
    ],
    [
        'name' => 'two',
        'age' => 12,
    ],
];

$array = [
    'users' => function () use ($users) {
        $new_users = [];
        foreach ($users as $user) {
            $new_users[] = array_merge(
                $user,
                [
                    'double_age' => $user['age'] * 2,
                ]
            );
        }

        return $new_users;
    },
];

ArrayToXml::convert($array)

This will result in:, (*23)

<?xml version="1.0"?>
<root>
    <users>
        <name>one</name>
        <age>10</age>
        <double_age>20</double_age>
    </users>
    <users>
        <name>two</name>
        <age>12</age>
        <double_age>24</double_age>
    </users>
</root>

Handling numeric keys

The package can also can handle numeric keys:, (*24)

$array = [
    100 => [
        'name' => 'Vladimir',
        'nickname' => 'greeflas',
    ],
    200 => [
        'name' => 'Marina',
        'nickname' => 'estacet',
    ],
];

$result = ArrayToXml::convert(['__numeric' => $array]);

This will result in:, (*25)

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <numeric_100>
        <name>Vladimir</name>
        <nickname>greeflas</nickname>
    </numeric_100>
    <numeric_200>
        <name>Marina</name>
        <nickname>estacet</nickname>
    </numeric_200>
</root>

You can change key prefix with setter method called setNumericTagNamePrefix()., (*26)

Using custom keys

The package can also can handle custom keys:, (*27)

$array = [
    '__custom:custom-key:1' => [
        'name' => 'Vladimir',
        'nickname' => 'greeflas',
    ],
    '__custom:custom-key:2' => [
        'name' => 'Marina',
        'nickname' => 'estacet',
        'tags' => [
            '__custom:tag:1' => 'first-tag',
            '__custom:tag:2' => 'second-tag',
        ]
    ],
];

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

This will result in:, (*28)

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <custom-key>
        <name>Vladimir</name>
        <nickname>greeflas</nickname>
    </custom-key>
    <custom-key>
        <name>Marina</name>
        <nickname>estacet</nickname>
        <tags>
            <tag>first-tag</tag>
            <tag>second-tag</tag>
        </tags>
    </custom-key>
</root>

A custom key contains three, colon-separated parts: "__custom:[custom-tag]:[unique-string]"., (*29)

  • "__custom"
    • The key always starts with "__custom".
  • [custom-tag]
    • The string to be rendered as the XML tag.
  • [unique-string]
    • A unique string that avoids overwriting of duplicate keys in PHP arrays.

a colon character can be included within the custom-tag portion by escaping it with a backslash:, (*30)

$array = [
    '__custom:ns\\:custom-key:1' => [
        'name' => 'Vladimir',
        'nickname' => 'greeflas',
    ],
    '__custom:ns\\:custom-key:2' => [
        'name' => 'Marina',
        'nickname' => 'estacet',
        'tags' => [
            '__custom:ns\\:tag:1' => 'first-tag',
            '__custom:ns\\:tag:2' => 'second-tag',
        ]
    ],
];

This will result in:, (*31)

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <ns:custom-key>
        <name>Vladimir</name>
        <nickname>greeflas</nickname>
    </ns:custom-key>
    <ns:custom-key>
        <name>Marina</name>
        <nickname>estacet</nickname>
        <tags>
            <ns:tag>first-tag</ns:tag>
            <ns:tag>second-tag</ns:tag>
        </tags>
    </ns:custom-key>
</root>

Setting DOMDocument properties

To set properties of the internal DOMDocument object just pass an array consisting of keys and values. For a full list of valid properties consult https://www.php.net/manual/en/class.domdocument.php., (*32)

You can use the constructor to set DOMDocument properties., (*33)

$result = ArrayToXml::convert(
   $array, 
   $rootElement, 
   $replaceSpacesByUnderScoresInKeyNames, 
   $xmlEncoding, 
   $xmlVersion, 
   ['formatOutput' => true]
);

Alternatively you can use setDomProperties, (*34)

$arrayToXml = new ArrayToXml($array);
$arrayToXml->setDomProperties(['formatOutput' => true]);
$result = $arrayToXml->toXml();

XML Prettification

Call $arrayToXml->prettify() method on ArrayToXml to set XML in pretty form., (*35)

Example:, (*36)

$array = [
    'Good guy' => [
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];
$arrayToXml = new ArrayToXml($array);

With prettification:, (*37)

$arrayToXml->prettify()->toXml();

will result in:, (*38)

<?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>

Without prettification:, (*39)

$arrayToXml->toXml();

will result in:, (*40)

<?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>

Dropping XML declaration

Call $arrayToXml->dropXmlDeclaration() method on ArrayToXml object to omit default XML declaration on top of the generated XML., (*41)

Example:, (*42)

$root = [
    'rootElementName' => 'soap:Envelope',
    '_attributes' => [
        'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/',
    ],
];
$array = [
    'soap:Header' => [],
    'soap:Body' => [
        'soap:key' => 'soap:value',
    ],
];
$arrayToXml = new ArrayToXml($array, $root);

$result = $arrayToXml->dropXmlDeclaration()->toXml();

This will result in:, (*43)

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header/><soap:Body><soap:key>soap:value</soap:key></soap:Body></soap:Envelope>

Adding processing instructions

Call $arrayToXml->addProcessingInstruction($target, $data) method on ArrayToXml object to prepend a processing instruction before the root element., (*44)

Example:, (*45)

$arrayToXml = new ArrayToXml($array);
$arrayToXml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');
$result = $arrayToXml->toXml();

This will result in:, (*46)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="base.xsl"?>
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>

Testing

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently., (*47)

Contributing

Please see CONTRIBUTING for details., (*48)

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities., (*49)

Postcardware

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

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium., (*51)

We publish all received postcards on our company website., (*52)

Credits

License

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

The Versions

04/05 2018

dev-master

9999999-dev https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

20/02 2018

dev-analysis-8bBPOa

dev-analysis-8bBPOa https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

20/02 2018

2.7.1

2.7.1.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

20/02 2018

dev-analysis-qgwm0d

dev-analysis-qgwm0d https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

07/09 2017

2.7.0

2.7.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

29/08 2017

2.6.1

2.6.1.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

25/08 2017

2.6.0

2.6.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

03/08 2017

2.5.2

2.5.2.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array convert

22/05 2017

2.5.0

2.5.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

 

The Development Requires

xml array convert

18/02 2017

2.3.0

2.3.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

xml array convert

08/12 2016

2.2.1

2.2.1.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php ^5.4|^7.0

 

The Development Requires

xml array convert

04/07 2016

2.2.0

2.2.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

23/02 2016

2.1.1

2.1.1.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

12/02 2016

2.1.0

2.1.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

08/10 2015

2.0.0

2.0.0.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

03/10 2015

1.0.3

1.0.3.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

24/05 2015

1.0.2

1.0.2.0 https://github.com/spatie/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

18/03 2015

1.0.1

1.0.1.0 https://github.com/freekmurze/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

17/03 2015

1.0.0

1.0.0.0 https://github.com/freekmurze/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert

17/03 2015

0.0.1

0.0.1.0 https://github.com/freekmurze/array-to-xml

Convert an array to xml

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

xml array convert