GithubHelp home page GithubHelp logo

rboonzaijer / php-array-to-xml Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 54 KB

Convert an array to XML with PHP

Home Page: https://packagist.org/packages/rboonzaijer/php-array-to-xml

License: MIT License

PHP 95.01% Roff 3.34% Shell 1.65%
array-to-xml array-to-xml-php xml array convert xml-document xml-files xml-output xml-schema array-xml

php-array-to-xml's Introduction

PhpArrayToXml

Convert an array to XML with PHP

License

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)

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

Usage

Basic example:

use RBoonzaijer\PhpArrayToXml\PhpArrayToXml;

$converter = new PhpArrayToXml();

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

Output:

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

Output Format (Prettify)

->setFormatOutput(bool $value = false)

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

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

Default:

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

Usage:

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

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

Result:

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

Custom root name

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

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

Result:

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

Custom tag name

Custom tag names are used when an array has no key names

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

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

Default (prettified):

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

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

Result (prettified):

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

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

Result (prettified):

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

XML encoding

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

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

Result (prettified):

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

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

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

Default (prettified):

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

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

Result (prettified):

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

->setTransformTags(string $value = null)

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

Default (prettified):

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

Usage (lowercase):

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

Result (prettified):

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

Usage (uppercase):

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

Result (prettified):

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

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

Result (prettified):

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

->setNumericTagSuffix(string $value = null)

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

Default (prettified):

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

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

Result (prettified):

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

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

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

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

Default (prettified):

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

Usage:

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

Result (prettified):

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

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

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

Default (prettified):

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

Usage:

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

Result (prettified):

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

Development

php-array-to-xml's People

Contributors

rboonzaijer avatar scrutinizer-auto-fixer avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

nicolasfrey

php-array-to-xml's Issues

Wrong default values for booleans and null values

Example array:

$array = [
  'StringTrue' => 'true',
  'StringFalse' => 'false',
  'BooleanTrue' => true,
  'BooleanFalse' => false,
  'StringNull' => 'null',
  'StringEmpty' => '',
  'RealNull' => null
];

Expected:

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

Actual result:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.