refactorstudio/php-array-to-xml

该包已被放弃且不再维护。作者建议使用rboonzaijer/php-array-to-xml包。

使用PHP将数组转换为XML

2.0.4 2024-01-15 16:21 UTC

README

使用PHP将数组转换为XML

68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f72626f6f6e7a61696a65722f7068702d61727261792d746f2d786d6c2f74657374732e796d6c3f6272616e63683d6d61696e License

68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e302d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e312d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e322d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e332d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d372e342d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e302d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e312d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e322d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265 68747470733a2f2f736869656c64732e696f2f62616467652f7374796c652d382e332d677265656e3f6c6162656c3d706870267374796c653d666c61742d737175617265

安装

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>

开发