susina / xml-to-array
一个简单的PHP库,用于将XML字符串转换为数组
Requires
- php: >=8.2
- ext-dom: *
- ext-libxml: *
- ext-simplexml: *
- symfony/options-resolver: ^7.1
Requires (Dev)
- mikey179/vfsstream: ^1.6
- pestphp/pest: ^2.35
- psalm/phar: ^5
- susina/coding-standard: ^2
This package is auto-updated.
Last update: 2024-09-15 15:51:08 UTC
README
Xml to Array 是一个简单的库,用于将XML转换为PHP数组。
该库包含一个命名空间 Susina\XmlToArray
和两个类
Converter
:将XML字符串转换为PHP数组FileConverter
:将XML文件转换为
两个类公开了相同的公共API
- Susina\XmlToArray\Converter
convert(string $xmlToParse): array
将XML字符串转换为数组convertAndSave(string $xmlToParse, string $filename): void
将XML字符串转换为数组并将其保存到常规PHP文件中。
- Susina\XmlToArray\FileConverter
convert(string $xmlFile): array
读取XML文件并将其转换为数组convertAndSave(string $xmlFile, string $filename): void
读取XML文件,将其转换为数组并将其保存到常规PHP文件中。
安装
通过 composer 安装库
composer require susina/xml-to-array
该库依赖于三个PHP扩展,通常默认安装
- libxml
- simplexml
- dom
以及 Symfony Options Resolver 组件,该组件将由composer安装。
使用方法
使用 convert
方法解析XML字符串。让我们看看以下示例
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = " <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>Star Wars</title> <starred>True</starred> <percentage>32.5</percentage> </movie> <movie> <title>The Lord Of The Rings</title> <starred>false</starred> <percentage>30.7</percentage> </movie> </movies> "; $converter = new Converter(); $array = $converter->convert($xmlString); /* * $array now contains the following array: * * [ * "movie" => [ * 0 => [ * "title" => "Star Wars", * "starred" => true, * "percentage" => 32.5 * ], * 1 => [ * "title" => "The Lord Of The Rings", * "starred" => false, * "percentage" => 30.7 * ] * ] * ] */
或者,您可以使用静态实例化器
<?php declare(strict_types=1); ..... $array = Converter::create()->convert($xmlString);
如果您想读取并转换 XML文件,您可以与 FileConverter
类一起使用
<?php declare(strict_types=1); use Susina\XmlToArray\FileConverter; $converter = new FileConverter(); $array = $converter->convert('/my_dir/my_file.xml');
并使用静态构造函数
<?php declare(strict_types=1); $array = FileConverter::create()->convert('/my_dir/my_file.xml');
您可以将转换后的数组保存到常规格式的PHP文件中,您可以通过 include PHP语句将其导入到其他脚本中。
例如
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = " <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>Star Wars</title> <starred>True</starred> <percentage>32.5</percentage> </movie> <movie> <title>The Lord Of The Rings</title> <starred>false</starred> <percentage>30.7</percentage> </movie> </movies> "; $converter = new Converter(); $converter->convertAndSave($xmlString, 'array_file.php');
array_file.php
的内容如下
<?php declare(strict_types=1); /* * This file is auto-generated by susina/xml-to-array library. */ return array ( 'movie' => array ( 0 => array ( 'title' => 'Star Wars', 'starred' => true, 'percentage' => 32.5, ), 1 => array ( 'title' => 'The Lord Of The Rings', 'starred' => false, 'percentage' => 30.7, ), ), );
您可以通过 include
语句加载您的数组
<?php declare(strict_types=1); //Some instructions $array = include('array_file.php');
此外,FileConverter
类还有其 convertAndSave
方法,它具有相同的行为,但接受要转换的XML文件名作为第一个参数
<?php declare(strict_types=1); ........... FileConverter::create()->convertAndSave($xmlFileName, 'array_file.php');
配置
您可以通过将关联数组传递给构造函数来配置转换器,其中键是选项的名称。可用选项如下
- mergeAttributes:布尔值,默认为true
- idAsKey:布尔值,默认为true
- typesAsString:布尔值,默认为false
- preserveFirstTag:布尔值,默认为false
mergeAttributes
默认:是
当此选项设置为true时,标签的属性将合并到标签本身,否则它们将保存到 @attribute
数组中,即
<?php declare(strict_types=1); $xmlString = ' <?xml version="1.0" encoding="utf-8"?> <config> <logger name="defaultLogger"> <type>stream</type> <path>/var/log/default.log</path> <level>300</level> </logger> </config> '; //mergeAttributes is true by default $array = Converter::create()->convert($xmlString); /* * $array now contains the following array: * * [ * "logger" => [ * "name" => "defaultLogger", * "type" => "stream", * "path" => "/var/log/default.log" * "level" => 300 * ] * ] */
在上一个示例中,您可以看到 name 属性被“合并”到 logger 数组中。
当此选项设置为false时,将创建一个 @attribute 数组
<?php declare(strict_types=1); $xmlString = ' <?xml version="1.0" encoding="utf-8"?> <config> <logger name="defaultLogger"> <type>stream</type> <path>/var/log/default.log</path> <level>300</level> </logger> </config> '; $array = Converter::create(['mergeAttributes' => false])->convert($xmlString); /* * $array now contains the following array: * * [ * "logger" => [ * "@attributes" => [ * "name" => "defaultLogger" * ], * "type" => "stream", * "path" => "/var/log/default.log" * "level" => 300 * ] * ] */
idAsKey
默认:是
当此选项设置为true时,id 属性或标签的值被视为关联数组的键,即
<?php declare(strict_types=1); $xmlString = " <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>Star Wars</title> <starred>True</starred> <actor id=\"actorH\" name=\"Harrison Ford\" /> <actor id=\"actorM\" name=\"Mark Hamill\" /> <actor> <id>actorC</id> <name>Carrie Fisher</name> </actor> </movie> </movies>"; //idAsKey is true by default $array = Converter::create()->convert($xmlString); /* * $array now contains the following array: * * 'movie' => [ * 0 => [ * 'title' => 'Star Wars', * 'starred' => true, * 'actorH' => ['name' => 'Harrison Ford'], * 'actorM' => ['name' => 'Mark Hamill'], * 'actorC' => ['name' => 'Carrie Fisher'] * ] * ] */
否则,如果您将此选项设置为 false,则不会发生任何特殊操作
<?php declare(strict_types=1); $xmlString = " <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>Star Wars</title> <starred>True</starred> <actor id=\"actorH\" name=\"Harrison Ford\" /> <actor id=\"actorM\" name=\"Mark Hamill\" /> <actor> <id>actorC</id> <name>Carrie Fisher</name> </actor> </movie> </movies>"; $converter = new Converter(['idAsKey' => false]); $array = $converter->convert($xmlString); /* * $array now contains the following array: * * 'movie' => [ * 0 => [ * 'title' => 'Star Wars', * 'starred' => true, * 'actor' => [ * 0 => [ * 'id' => 'actorH', * 'name' => 'Harrison Ford' * ], * 1 => [ * 'id' => 'actorM', * 'name' => 'Mark Hamill' * ], * 2 => [ * 'id' => 'actorC', * 'name' => 'Carrie Fisher' * ] * ] * ] * ] */
typesAsString
默认:否
该库的正常行为是保留所有PHP类型(布尔值、数值、null等)。如果 typesAsString 选项设置为 true,则所有值都视为 字符串
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = " <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>Star Wars</title> <starred>True</starred> <percentage>32.5</percentage> <views>589623</views> </movie> </movies> "; // typesAsString is false by default $array = Converter::create()->convert($xmlString); /* * $array now contains the following array: * * [ * "movie" => [ * 0 => [ * "title" => "Star Wars", * "starred" => true, * "percentage" => 32.5, * "views" => 589623 * ], * ] * ] */
在之前的示例中,如果您将此属性设置为true,则所有值都是字符串
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = ....... $array = Converter::create(['typesAsString' => true])->convert($xmlString); /* * $array now contains the following array: * * [ * "movie" => [ * 0 => [ * "title" => "Star Wars", * "starred" => 'True', * "percentage" => '32.5' * "views" => '589623' * ], * ] * ] */
preserveFirstTag
默认:否
如果您的XML文档以一个包含所有其他标签的单个标签开始,则第一个标签不会被考虑为结果数组的一部分
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = " <?xml version='1.0' standalone='yes'?> <database> <movie> <title>Star Wars</title> </movie> <movie> <title>The Lord Of The Rings</title> </movie> <movie> <title>Spider-Man</title> </movie> </database> "; // preserveFirstTag is false by default $converter = new Converter(); $array = $converter->convert($xmlString); /* * $array now contains the following array: * * [ * "movie" => [ * 0 => [ * "title" => "Star Wars", * ], * 1 => [ * "title" => "The Lord Of The Rings", * ], * 2 => [ * "title" "Spider-Man" * ] * ] * ] */
如果您想将此标签保留为您的数组的第一键,请将此选项设置为true
<?php declare(strict_types=1); use Susina\XmlToArray\Converter; $xmlString = ............. $converter = new Converter(['preserveFirstTag' => true]); $array = $converter->convert($xmlString); /* * $array now contains the following array: * * [ * "database => [ * "movie" => [ * 0 => [ * "title" => "Star Wars", * ], * 1 => [ * "title" => "The Lord Of The Rings", * ], * 2 => [ * "title" "Spider-Man" * ] * ] * ] * ] */
问题
如果您发现错误或其他问题,请在Github上报告。
贡献
许可
此库在Apache-2.0许可下发布。