XML 转 数组,数组 转 XML

1.0 2022-06-09 10:58 UTC

This package is auto-updated.

Last update: 2024-09-09 15:48:53 UTC


README

此库提供了一种最简单、最直接的方法在 XML 和数组之间进行切换。

要求

  • PHP 5.6 或更高版本
  • PHP DOM 扩展

安装

composer require muhammetsafak/toxml

配置

以下数组是一个关联配置数组,声明了可识别的配置。它可以在创建对象时定义,也可以在之后使用设置方法定义。

array(
    'encode'            => 'UTF-8', // String
    'version'           => '1.0', // String ("1.0" or "1.1")
    'attributesKey'     => '@attibutes', // String
    'cdataKey'          => '@cdata', // String
    'valueKey'          => '@value', // String
    'useNamespaces'     => false, // Boolean
    'rootNodeName'      => 'root', // String
);

用法

数组 转 XML

require_once "vendor/autoload.php";

$data = array();

$xml = new \MuhammetSafak\ToXML\XML();

header('Content-Type: application/xml; charset=utf-8');
echo $xml->withArray($data)
        ->toXML();

XML 转 数组

require_once "vendor/autoload.php";

$source = file_get_contents('example.xml');

$xml = new \MuhammetSafak\ToXML\XML();
$data = $xml->withXML($source)
            ->toArray();

鸣谢

许可证

版权所有 © 2022 MIT 许可证