PHP code example of php-junior / laravel-xml

1. Go to this page and download the library: Download php-junior/laravel-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/ */

    

php-junior / laravel-xml example snippets


use Illuminate\Support\Arr;

$data = [
    'name' => 'John Doe',
    'description' => 'This is a <strong>description</strong>',
    'bio01' => 'This is a <strong>bio01</strong>',
    'bio02' => 'This is a <strong>bio02</strong>',
    'bio03' => 'This is a <strong>bio03</strong>',
    'bio04' => 'This is a <strong>bio04</strong>',
];

$xml = Arr::toXml($data);
<root>
    <name>John Doe</name>
    ...
</root>

// change root tag
$xml = Arr::toXml($data, 'user');
<user>
    <name>John Doe</name>
    ...
</user>

// cdata tag
$xml = Arr::toXml($data, 'user', ['description']);
<user>
    <name>John Doe</name>
    <description><![CDATA[This is a <strong>description</strong>]]></description>
    ...
</user>

// cdata wildcard tag
$xml = Arr::toXml($data, 'user', ['bio*']);
<user>
    <name>John Doe</name>
    <bio01><![CDATA[This is a <strong>bio01</strong>]]></bio01>
    <bio02><![CDATA[This is a <strong>bio02</strong>]]></bio02>
    <bio03><![CDATA[This is a <strong>bio03</strong>]]></bio03>
    <bio04><![CDATA[This is a <strong>bio04</strong>]]></bio04>
</user>

// multi-dimensional array
Arr::toXml([
    'user' => [
        [
            'name' => 'User',
            'email' => '[email protected]'
        ],
        [
            'name' => 'User 2',
            'email' => '[email protected]'
        ],
    ]
], 'users');
<users>
    <user>
        <name>User</name>
        <email>[email protected]</email>
    </user>
    <user>
        <name>User 2</name>
        <email>[email protected]</email>
    </user>
</users>

// xml to array
$data = Arr::fromXml($xml);
[
    'name' => 'John Doe',
    'description' => 'This is a <strong>description</strong>',
    'bio01' => 'This is a <strong>bio01</strong>',
    'bio02' => 'This is a <strong>bio02</strong>',
    'bio03' => 'This is a <strong>bio03</strong>',
    'bio04' => 'This is a <strong>bio04</strong>',
]
bash
composer