rboonzaijer/php-array-to-xml

使用 PHP 将数组转换为 XML

2.0.4 2024-01-15 16:21 UTC

This package is auto-updated.

Last update: 2024-09-23 17:52:41 UTC


README

使用 PHP 将数组转换为 XML

License

安装

composer require rboonzaijer/php-array-to-xml ^2.0

需要供应商文件(如果你使用的是类似 Symfony 或 Laravel 的框架,这些文件已经加载)

require __DIR__ . '/vendor/autoload.php';

使用

基本示例

use RBoonzaijer\PhpArrayToXml\PhpArrayToXml;

$converter = new PhpArrayToXml();

$result = $converter->toXmlString(['title' => 'My Products']);

输出

<?xml version="1.0" encoding="UTF-8"?>
<root><title>My Products</title></root>

输出格式(美化)

->setFormatOutput(bool $value = false)

别名: ->prettify() 等同于 ->setFormatOutput(true)

$array = [
  'title' => 'My Products',
  'pricing' => 'Pricing'
];

默认

<?xml version="1.0" encoding="UTF-8"?>
<root><title>My Products</title><pricing>Pricing</pricing></root>

使用

$result = $converter->setFormatOutput(true)->toXmlString($array);

// or use the alias:
$result = $converter->prettify()->toXmlString($array);

结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <title>My Products</title>
  <pricing>Pricing</pricing>
</root>

自定义根名称

->setCustomRootName(string $value = 'root')

$result = $converter->setCustomRootName('data')->toXmlString();

结果

<?xml version="1.0" encoding="UTF-8"?>
<data>
  ...
</data>

自定义标签名称

当数组没有键名称时使用自定义标签名称

->setCustomTagName(string $value = 'node')

$array = [
  'title' => 'My Products',
  'products' => [
    [
      'name' => 'Raspberry Pi 3',
      'price' => 39.99
    ],
    [
      'name' => 'Arduino Uno Rev3',
      'price' => 19.99
    ]
  ]
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <title>My Products</title>
  <products>
    <node>
      <name>Raspberry Pi 3</name>
      <price>39.99</price>
    </node>
    <node>
      <name>Arduino Uno Rev3</name>
      <price>19.99</price>
    </node>
  </products>
</root>

使用

$xml_string = $converter->setCustomTagName('item')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <title>My Products</title>
  <products>
    <item>
      <name>Raspberry Pi 3</name>
      <price>39.99</price>
    </item>
    <item>
      <name>Arduino Uno Rev3</name>
      <price>19.99</price>
    </item>
  </products>
</root>

XML 版本

->setVersion(string $value = '1.0')

$xml_string = $converter->setVersion('1.1')->toXmlString(['test']);

结果(美化)

<?xml version="1.1" encoding="UTF-8"?>
<root>
  <node>test</node>
</root>

XML 编码

->setEncoding(string $value = 'UTF-8')

$xml_string = $converter->setEncoding('ISO-8859-1')->toXmlString(['test']);

结果(美化)

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <node>test</node>
</root>

标签分隔符

设置用于替换标签名称中特殊字符的分隔符值

->setSeparator(string $value = '_')

$array = [
  'some of these keys have' => 'My Value 1',
  'spaces in them' => 'My Value 2',
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <some_of_these_keys_have>My Value 1</some_of_these_keys_have>
  <spaces_in_them>My Value 2</spaces_in_them>
</root>

使用

$xml_string = $converter->setSeparator('-')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <some-of-these-keys-have>My Value 1</some-of-these-keys-have>
  <spaces-in-them>My Value 2</spaces-in-them>
</root>

转换标签名称

将标签名称转换为大写/小写

->setTransformTags(string $value = null)

$array = [
  'This' => [
    'Is' => [
      'an',
      'Example'
    ]
  ]
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <This>
    <Is>
      <node>an</node>
      <node>Example</node>
    </Is>
  </This>
</root>

使用(小写)

$xml_string = $converter->setTransformTags('lowercase')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <this>
    <is>
      <node>an</node>
      <node>Example</node>
    </is>
  </this>
</root>

使用(大写)

$xml_string = $converter->setTransformTags('uppercase')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <THIS>
    <IS>
      <NODE>an</NODE>
      <NODE>Example</NODE>
    </IS>
  </THIS>
</ROOT>

使用(大写,但具有自定义标签名称,这些名称将不会转换)

$xml_string = $converter
              ->setTransformTags('uppercase')
              ->setCustomRootName('MyRoot')
              ->setCustomTagName('MyCustomTag')
              ->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<MyRoot>
  <THIS>
    <IS>
      <MyCustomTag>an</MyCustomTag>
      <MyCustomTag>Example</MyCustomTag>
    </IS>
  </THIS>
</MyRoot>

设置数字标签后缀

如果不为 null,则将数字数组键追加到标签名称中,值作为分隔符。

->setNumericTagSuffix(string $value = null)

$array = [
  'this',
  'is',
  'an'
  [
    'example',
    'using',
    'numeric tag suffix',
  ],
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <node>this</node>
  <node>is</node>
  <node>an</node>
  <node>
    <node>example</node>
    <node>using</node>
    <node>numeric tag suffix</node>
  </node>
</root>

使用

$xml_string = $converter->setNumericTagSuffix('_')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <node_0>this</node_0>
  <node_1>is</node_1>
  <node_2>an</node_2>
  <node_3>
    <node_0>example</node_0>
    <node_1>using</node_1>
    <node_2>numeric tag suffix</node_2>
  </node_3>
</root>

转换布尔值

默认情况下,数组中的布尔值将被转换为字符串 'true' 或 'false'。您可以选择将其转换为任何您喜欢的(字符串)值。此方法仅适用于真实的布尔值,因此具有值 'true' 和 'false' 的字符串不受影响。

->setCastBooleanValueTrue(string $value = 'true')

->setCastBooleanValueFalse(string $value = 'false')

$array = [
  'StringTrue' => 'true',
  'StringFalse' => 'false',
  'BooleanTrue' => true,
  'BooleanFalse' => false
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <StringTrue>true</StringTrue>
  <StringFalse>false</StringFalse>
  <BooleanTrue>true</BooleanTrue>
  <BooleanFalse>false</BooleanFalse>
</root>

使用

$xml_string = $converter->setCastBooleanTrue('Yes')->setCastBooleanFalse('No')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <StringTrue>true</StringTrue>
  <StringFalse>false</StringFalse>
  <BooleanTrue>Yes</BooleanTrue>
  <BooleanFalse>No</BooleanFalse>
</root>

转换 NULL 值

默认情况下,数组中的 NULL 值在 XML 中没有值,因此标签看起来像这样:<MyTag/>。您可以选择将其转换为任何您喜欢的(字符串)值。此方法仅适用于真实的 'null' 值,因此具有值 'null' 或空字符串 '' 的字符串不受影响。

->setCastNullValue(null|string $value = null)

$array = [
  'StringNull' => 'null',
  'StringEmpty' => '',
  'RealNull' => null
];

默认(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <StringNull>null</StringNull>
  <StringEmpty/>
  <RealNull/>
</root>

使用

$xml_string = $converter->setCastNullValue('__NULL__')->setCastBooleanFalse('No')->toXmlString($array);

结果(美化)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <StringNull>null</StringNull>
  <StringEmpty/>
  <RealNull>__NULL__</RealNull>
</root>

开发