clearstream/xml-to-array 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

clearstream/xml-to-array

最新稳定版本:0.4.0

Composer 安装命令:

composer require clearstream/xml-to-array

包简介

Incredibly simple XML to array PHP converter

README 文档

README

This package provides a very simple class to convert XML string to array.

Why should I use this package?

  • because you don't like XML
  • because other packages generate inconsistent array structure
  • because you can't serialize SimpleXML objects (but can serialize arrays)
  • because you just want to use arrays

Install

You can install this package via composer.

composer require clearstream/xml-to-array

Usage

use Clearstream\XmlToArray\XmlToArray;

$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<response success="true">
    <users>
        <user id="1" name="Illia" />
        <user id="2" name="Trevor" />
    </users>
    <settings>
        <mode value="light" />
        <color value="purple" />
    </settings>
    <article id="1">
        Hello World!
    </article>
    <empty></empty>
    <cdata><![CDATA[<sender>John Doe</sender>]]></cdata>
</response>
XML;

$array = XmlToArray::convert($xml);

After running this piece of code $array will contain:

[
    'response' => [
        '@success' => 'true',
        '#text' => '',
        'users' => [
            [
                '#text' => '',
                'user' => [
                    [
                        '@id' => '1',
                        '@name' => 'Illia',
                        '#text' => '',
                    ],
                    [
                        '@id' => '2',
                        '@name' => 'Trevor',
                        '#text' => '',
                    ],
                ],
            ],
        ],
        'settings' => [
            [
                '#text' => '',
                'mode' => [
                    [
                        '@value' => 'light',
                        '#text' => '',
                    ],
                ],
                'color' => [
                    [
                        '@value' => 'purple',
                        '#text' => '',
                    ],
                ],
            ],
        ],
        'article' => [
            [
                '@id' => '1',
                '#text' => 'Hello World!',
            ],
        ],
        'empty' => [
            ['#text' => ''],
        ],
        'cdata' => [
            ['#text' => '<sender>John Doe</sender>'],
        ],
    ],
]

If your input contains something that cannot be parsed a \Clearstream\XmlToArray\XmlToArrayException will be thrown.

Testing

vendor/bin/phpunit

License

GNU GENERAL PUBLIC LICENSE. Please see License File for more information.

统计信息

  • 总下载量: 35.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 66
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 66
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2021-03-11