2017 © Pedro Peláez
 

library php-array-to-xml

Convert an array to XML with PHP

image

refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  • Saturday, April 21, 2018
  • by refactorstudio
  • Repository
  • 3 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 20 % Grown

The README.md

PhpArrayToXml

Convert an array to XML with PHP, (*1)

License

, (*2)

Install

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

Require the vendor files (these already loaded if you are using something like Symfony or Laravel), (*3)

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

Usage

Basic example:, (*4)

use RBoonzaijer\PhpArrayToXml\PhpArrayToXml;

$converter = new PhpArrayToXml();

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

Output:, (*5)

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

Output Format (Prettify)

->setFormatOutput(bool $value = false), (*6)

Alias: ->prettify() is the same as typing: ->setFormatOutput(true), (*7)

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

Default:, (*8)

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

Usage:, (*9)

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

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

Result:, (*10)

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

Custom root name

->setCustomRootName(string $value = 'root'), (*11)

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

Result:, (*12)

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

Custom tag name

Custom tag names are used when an array has no key names, (*13)

->setCustomTagName(string $value = 'node'), (*14)

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

Default (prettified):, (*15)

<?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>

Usage:, (*16)

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

Result (prettified):, (*17)

<?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 version

->setVersion(string $value = '1.0'), (*18)

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

Result (prettified):, (*19)

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

XML encoding

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

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

Result (prettified):, (*21)

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

Tag separator

Set the value for the separator that will be used to replace special characters in tag names, (*22)

->setSeparator(string $value = '_'), (*23)

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

Default (prettified):, (*24)

<?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>

Usage:, (*25)

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

Result (prettified):, (*26)

<?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>

Transform tag names

Transform tag names to uppercase/lowercase, (*27)

->setTransformTags(string $value = null), (*28)

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

Default (prettified):, (*29)

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

Usage (lowercase):, (*30)

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

Result (prettified):, (*31)

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

Usage (uppercase):, (*32)

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

Result (prettified):, (*33)

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

Usage (uppercase, but with custom tag names, which will not be transformed):, (*34)

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

Result (prettified):, (*35)

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

Set numeric tag suffix

If this is not null, it appends the numeric array key to the tag name, with the value as separator., (*36)

->setNumericTagSuffix(string $value = null), (*37)

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

Default (prettified):, (*38)

<?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>

Usage:, (*39)

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

Result (prettified):, (*40)

<?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>

Cast boolean values

By default boolean values from the array will be cast to the string 'true' or 'false'. You can choose to cast it to any (string) value you like. This method only works on real boolean values, so strings with the value 'true' and 'false' are untouched., (*41)

->setCastBooleanValueTrue(string $value = 'true'), (*42)

->setCastBooleanValueFalse(string $value = 'false'), (*43)

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

Default (prettified):, (*44)

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

Usage:, (*45)

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

Result (prettified):, (*46)

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

Cast NULL values

By default null values from the array will have no value in the XML, so the tag looks something like this: <MyTag/>. You can choose to cast it to any (string) value you like. This method only works on real 'null' values, so strings with the value 'null' or empty strings '' are untouched., (*47)

->setCastNullValue(null|string $value = null), (*48)

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

Default (prettified):, (*49)

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

Usage:, (*50)

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

Result (prettified):, (*51)

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

Development

The Versions

21/04 2018

dev-master

9999999-dev https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

21/04 2018

1.1.1

1.1.1.0 https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

21/04 2018

dev-scrutinizer-patch-2

dev-scrutinizer-patch-2 https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

21/04 2018

dev-improve-code-quality

dev-improve-code-quality https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

21/04 2018

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1 https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

20/04 2018

dev-Scrutinizer

dev-Scrutinizer https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

parser xml array convert php-array-to-xml refactorstudio array-to-xml array-to-xml-php

15/04 2018

1.1.0

1.1.0.0 https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array converter

15/04 2018

1.0.0

1.0.0.0 https://github.com/refactorstudio/php-array-to-xml

Convert an array to XML with PHP

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

xml array converter