PHP code example of darkfriend / php7-xml

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

    

darkfriend / php7-xml example snippets


$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
        'cdata' => 'this is long text',
    ],
    'qpo' => [
        '@attributes' => [
            'test1' => 'valTest',
        ],
        'value' => [
            'sub1' => [
                'value' => [
                    'sub2'=>'val'
                ]
            ],
            'multi-sub2' => [
                [
                    '@attributes' => [
                        'at1' => 'val'
                    ],
                    'value' => [
                        'multi-sub2-sub3' => [
                            'value' => 'value multi-sub2'
                        ]
                    ]
                ],
                [
                    'value' => 'value multi-sub2'
                ],
            ],
        ],
    ],
    'duplicateElement' => [
        [
            '@attributes' => [
                'atDuplicate1' => 'val'
            ],
            'value' => 'valueDuplicateElement1',
        ],
        [
            '@attributes' => [
                'atDuplicate2' => 'val'
            ],
            'value' => 'valueDuplicateElement2',
        ],
        [
            '@attributes' => [
                'atDuplicate3' => 'val'
            ],
            'value' => [
                'subElement' => 'val'
            ],
        ],
    ]
];

echo \darkfriend\helpers\Xml::encode($array);

// example with use events
\darkfriend\helpers\Xml::encode($array, [
    'afterChild' => function($xml, $child, $name, $params) {
        // your code
        return $child;
    },
]);

$xml = '<?xml version="1.0" encoding="utf-8"

$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
        'value' => 'this is long text',
    ],
];

echo \darkfriend\helpers\Xml::encode(
    $array,
    [
        'root' => '<main atExample="atValue"/>', // custom root element with custom attribute atExample
        'prolog' => [
            'attributes' => [
                'version' => '1.0',
                'encoding' => 'utf-8',
            ],
            'elements' => [ // additional elements for prolog
                /*'<?xml-stylesheet type="text/css" href="/style/design"

$array = [
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => [
        'cdata' => 'this is long text',
        '@attributes' => [
            'at1' => 'at1val',
            'at2' => 'at2val',
        ],
    ]
];

echo \darkfriend\helpers\Xml::encode(
    $array,
    [
        'root' => '<response/>',
    ]
);