rufov / converter-array-xml
该类设计用于将XML文档转换为PHP数组,反之亦然
资助包维护!
rufovS
vk.com/rufow
Open Collective
Requires
- php: >=8.1
- ext-dom: *
- spatie/array-to-xml: ^3.1
Requires (Dev)
- mockery/mockery: ^1.2
- pestphp/pest: ^1.21
- spatie/pest-plugin-snapshots: ^1.1
This package is auto-updated.
Last update: 2024-09-09 11:26:28 UTC
README
该类设计用于将XML文档转换为PHP数组,反之亦然
安装
可以使用composer安装此包
composer require rufov/converter-array-xml
用法
use RufovS\ConverterArrayXML\ConverterArrayXML; $xml = <<<_XML <?xml version="1.0"?> <root xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <GoodGuy attr1="value"> <name><![CDATA[<h1>Luke Skywalker</h1>]]></name> <weapon>Lightsaber</weapon> </GoodGuy> <BadGuy> <name>Sauron</name> <weapon>Evil Eye</weapon> </BadGuy> </root> _XML; $result = ConverterArrayXML::xmlToArr($xml);
运行此段代码后,$result
将包含
$result = [ 'root' => [ '_attributes' => [ 'xmlns:xs=' => 'http://www.w3.org/2001/XMLSchema', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' ], 'GoodGuy' => [ '_attributes' => [ 'attr1' => 'value' ], 'name' => [ '_cdata' => '<h1>Luke Skywalker</h1>' ], 'weapon' => 'Lightsaber' ], 'BadGuy' => [ 'name' => 'Sauron', 'weapon' => 'Evil Eye' ] ] ];
您还可以将数组转换为XML。操作如下
use RufovS\ConverterArrayXML\ConverterArrayXML; $array = [ 'root' => [ '_attributes' => [ 'xmlns:xs=' => 'http://www.w3.org/2001/XMLSchema', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' ], 'GoodGuy' => [ '_attributes' => [ 'attr1' => 'value' ], 'name' => [ '_cdata' => '<h1>Luke Skywalker</h1>' ], 'weapon' => 'Lightsaber' ], 'BadGuy' => [ 'name' => 'Sauron', 'weapon' => 'Evil Eye' ] ] ]; $result = ConverterArrayXML::arrayToXml($array);
最后您将得到以下XML文档。
<?xml version="1.0"?> <root xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <GoodGuy attr1="value"> <name><![CDATA[<h1>Luke Skywalker</h1>]]></name> <weapon>Lightsaber</weapon> </GoodGuy> <BadGuy> <name>Sauron</name> <weapon>Evil Eye</weapon> </BadGuy> </root>
测试
vendor/bin/phpunit
贡献者
感谢所有已经做出贡献的人们!
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
许可证
BSD-3-Clause许可证。请参阅许可证文件以获取更多信息。