rafrsr/lib-array2xml
Array2XML 转换库
v1.0.1
2018-04-13 12:46 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ^4.4
- satooshi/php-coveralls: ^1.0
This package is auto-updated.
Last update: 2024-09-15 11:01:42 UTC
README
XML2Array 是一个 PHP 中的类,可以将 XML 转换为数组。它返回一个数组,可以使用 Array2XML 类将其转换回 XML。
它可以将字符串 XML 或 DOMDocument 类型的对象作为输入。
安装
- 安装 composer
- 执行:
composer require rafrsr/lib-array2xml
使用方法
使用方法非常简单。您需要在代码中包含类文件,并调用以下函数。
$array = XML2Array::createArray($xml); print_r($array);
示例
以下 XML
<?xml version="1.0" encoding="UTF-8"?> <movies type="documentary"> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El ActÓr</actor> </character> </characters> <plot><![CDATA[So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.]]></plot> <great-lines> <line>PHP solves all my web problems</line> </great-lines> <rating type="thumbs">7</rating> <rating type="stars">5</rating> </movie> </movies>
将生成以下输出
array ( 'movies' => array ( 'movie' => array ( 'title' => 'PHP: Behind the Parser', 'characters' => array ( 'character' => array ( 0 => array ( 'name' => 'Ms. Coder', 'actor' => 'Onlivia Actora', ), 1 => array ( 'name' => 'Mr. Coder', 'actor' => 'El ActÓr', ), ), ), 'plot' => array ( '@cdata' => 'So, this language. It\'s like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.', ), 'great-lines' => array ( 'line' => 'PHP solves all my web problems', ), 'rating' => array ( 0 => array ( '@value' => '7', '@attributes' => array ( 'type' => 'thumbs', ), ), 1 => array ( '@value' => '5', '@attributes' => array ( 'type' => 'stars', ), ), ), ), '@attributes' => array ( 'type' => 'documentary', ), ), )
参考
此类基于 http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array/,修复了一些小错误并支持 php7
版权
此项目采用 MIT 许可证。