aivec / array-to-xml
将数组转换为xml
Requires
- php: >=5.6
- ext-dom: *
Requires (Dev)
README
此包提供了一个将数组转换为XML字符串的非常简单的类。
PHP 5.6 兼容
这是一个https://github.com/spatie/array-to-xml的分支,主要是为了与较旧的PHP版本兼容。
安装
您可以通过composer安装此包。
composer require aivec/array-to-xml
用法
use aivec\ArrayToXml\ArrayToXml; ... $array = [ 'Good guy' => [ 'name' => 'Luke Skywalker', 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => 'Sauron', 'weapon' => 'Evil Eye' ] ]; $result = ArrayToXml::convert($array);
运行此段代码后,$result
将包含
<?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>
设置根元素的名称
您可以选择将rootElement的名称作为第二个参数传递来设置rootElement的名称。如果您不指定此参数(或将它设置为空字符串),则将使用"root"。
$result = ArrayToXml::convert($array, 'customrootname');
处理键名
默认情况下,您数组键名中的所有空格都将转换为下划线。如果您想放弃这种行为,可以将第三个参数设置为false。我们将保留所有键名。
$result = ArrayToXml::convert($array, 'customrootname', false);
添加属性
您可以使用名为_attributes
的键来添加属性到节点,并使用_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);
此代码将产生
<?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>
使用保留字符
您还可以将节点的值包装到CDATA部分中。这允许您使用保留字符。
$array = [ 'Good guy' => [ 'name' => [ '_cdata' => '<h1>Luke Skywalker</h1>' ], 'weapon' => 'Lightsaber' ], 'Bad guy' => [ 'name' => '<h1>Sauron</h1>', 'weapon' => 'Evil Eye' ] ]; $result = ArrayToXml::convert($array);
此代码将产生
<?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>
如果您的输入包含无法解析的内容,将抛出DOMException
。
添加根元素的属性
要添加根元素的属性,请提供第二个参数为一个包含_attributes
键的数组。然后可以使用rootElementName
键设置根元素名称。
$result = ArrayToXml::convert($array, [ 'rootElementName' => 'helloyouluckypeople', '_attributes' => [ 'xmlns' => 'https://github.com/aivec/array-to-xml', ], ], true, 'UTF-8');
使用多维数组
使用多维数组来创建元素集合。
$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'], ], ], ];
这将产生
<?xml version="1.0" encoding="UTF-8"?> <helloyouluckypeople xmlns="https://github.com/aivec/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>
处理数字键
该包还可以处理数字键
$array = [ 100 => [ 'name' => 'Vladimir', 'nickname' => 'greeflas', ], 200 => [ 'name' => 'Marina', 'nickname' => 'estacet', ], ]; $result = ArrayToXml::convert(['__numeric' => $array]);
这将产生
<?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>
您可以使用名为setNumericTagNamePrefix()
的setter方法来更改键前缀。
设置DOMDocument属性
要设置内部DOMDocument对象的属性,请传递一个由键和值组成的数组。有关有效属性的完整列表,请参阅https://php.ac.cn/manual/en/class.domdocument.php。
您可以使用构造函数来设置DOMDocument属性。
$result = ArrayToXml::convert( $array, $rootElement, $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion, ['formatOutput' => true] );
或者您可以使用setDomProperties
$arrayToXml = new ArrayToXml($array); $arrayToXml->setDomProperties(['formatOutput' => true]); $result = $arrayToXml->toXml();
测试
vendor/bin/phpunit
变更日志
请参阅CHANGELOG获取最近更改的更多信息。
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何安全问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。
明信片软件
您可以使用此包,但如果它进入了您的生产环境,我们非常感谢您从您的家乡给我们寄一张明信片,并说明您正在使用我们的哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将发布所有收到的明信片在我们的公司网站上。
鸣谢
支持我们
Spatie是一家位于比利时的安特卫普的网络设计公司。您可以在我们的网站上找到我们所有开源项目的概述在这里。
您的业务是否依赖我们的贡献?在 Patreon 上联系我们并支持我们。所有承诺都将专门用于分配人力资源以进行维护和新酷炫功能。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。