PHP code example of mblarsen / array-xml

1. Go to this page and download the library: Download mblarsen/array-xml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mblarsen / array-xml example snippets


ArrayToXML::toXML(
    'Order@version=2.0' => [
        'ID@type=SKU' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines|OrderLine' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ],
    [
        'name_mappers' => [
            'Line' => function ($name, $index, $value) {
                return $name . '@number=' . ($index + 1);
            }
        ]
    ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        // Node names will be 'Line'
        '<Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ],
        // Node name will be 'Lines'
        // (some XSDs actually pluralize 'things' 🤦)
        '<Foo|Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ],
    ],
);

$complex_dom = ...;

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        '<complexdom' => $complexdom
    ],
);

ArrayToXML::toXML(
    'Order@ns=...@ecom=...' => [
        'ns:ID' => 1234,
        '<ecom:Lines' => [
            ['ecom:item' => 'ABC', 'ecom:qty' => 3],
            ['ecom:item' => 'DEF', 'ecom:qty' => 1],
        ]
    ],
);

ArrayToXML::toDOM(
    'Order' => [
        'ID' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ],
    [
        'version' => '1.2',
        'encoding' => 'utf8'
    ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ],
    [ 'declare' => false ]
);

ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Comment' => 'cdata:foo',
    ]
);
xml
<Order>
  <ID>1234</ID>
  <Lines>
    <Line>
      <item>ABC</item>
      <qty>3</qty>
    </Line>
    <Line>
      <item>DEF</item>
      <qty>1</qty>
    </Line>
  </Lines>
</Order>