This package provides a very simple class to convert an array to an xml string.
<img src="https://github-ads.s3.eu-central-1.amazonaws.com/array-to-xml.jpg?t=1" width="419px" />
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.
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.
You can install this package via composer.
composer require spatie/array-to-xml
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, and _value to specify the value.
$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:
<?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)
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
$array = [ 'Good guy' => [ 'name' => [ '_cdata' => '<h1>Luke Skywalker</h1>' ], 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => '<h1>Sauron</h1>', 'weapon' => 'Evil Eye' ] ]; $result = ArrayToXml::convert($array);
This code will result in:
<?xml version="1.0"?> <root> <Good_guy> <name><![CDATA[<h1>Luke Skywalker</h1>]]></name> <weapon>Lightsaber</weapon> </Good_guy> <Bad_guy> <name><h1>Sauron</h1></name> <weapon>Evil Eye</weapon> </Bad_guy> </root>
If your input contains something that cannot be parsed a DOMException will be thrown.
You could specify specific values in for:
$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], true);
This will result in:
<?xml version="1.1" encoding="UTF-8" standalone="yes"?>
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' => 'helloyouluckypeople', '_attributes' => [ 'xmlns' => 'https://github.com/spatie/array-to-xml', ], ], true, 'UTF-8');
Use a multi-dimensional array to create a collection of elements.
$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:
<?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>
The package can use Closure values:
$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:
<?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>
The package can also can handle numeric keys:
$array = [ 100 => [ 'name' => 'Vladimir', 'nickname' => 'greeflas', ], 200 => [ 'name' => 'Marina', 'nickname' => 'estacet', ], ]; $result = ArrayToXml::convert(['__numeric' => $array]);
This will result in:
<?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().
The package can also can handle custom keys:
$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:
<?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]".
a colon character can be included within the custom-tag portion by escaping it with a backslash:
$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:
<?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>
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.
You can use the constructor to set DOMDocument properties.
$result = ArrayToXml::convert( $array, $rootElement, $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion, ['formatOutput' => true] );
Alternatively you can use setDomProperties
$arrayToXml = new ArrayToXml($array); $arrayToXml->setDomProperties(['formatOutput' => true]); $result = $arrayToXml->toXml();
Call $arrayToXml->prettify() method on ArrayToXml to set XML in pretty form.
Example:
$array = [ 'Good guy' => [ 'name' => 'Luke Skywalker', 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => 'Sauron', 'weapon' => 'Evil Eye' ] ]; $arrayToXml = new ArrayToXml($array);
With prettification:
$arrayToXml->prettify()->toXml();
will result in:
<?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:
$arrayToXml->toXml();
will result in:
<?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>
Call $arrayToXml->dropXmlDeclaration() method on ArrayToXml object to omit default XML declaration on top of the generated XML.
Example:
$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:
<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>
Call $arrayToXml->addProcessingInstruction($target, $data) method on ArrayToXml object to prepend a processing instruction before the root element.
Example:
$arrayToXml = new ArrayToXml($array); $arrayToXml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"'); $result = $arrayToXml->toXml();
This will result in:
<?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>
vendor/bin/phpunit
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
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.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The MIT License (MIT). Please see License File for more


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法 律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以 让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。


选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感 知和代码自动完成功能,是提升开发效率的理想工具。


最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。


像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。


AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号