PHP code example of darkfriend / php5-xml

1. Go to this page and download the library: Download darkfriend/php5-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 / php5-xml example snippets


$array = array(
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => array(
        '@attributes' => array(
            'at1' => 'at1val',
            'at2' => 'at2val',
        ),
        'cdata' => 'this is long text',
    ),
    'qpo' => array(
        '@attributes' => array(
            'test1' => 'valTest',
        ),
        'value' => array(
            'sub1' => array(
                'value' => array(
                    'sub2'=>'val'
                )
            ),
            'multi-sub2' => array(
                array(
                    '@attributes' => array(
                        'at1' => 'val'
                    ),
                    'value' => array(
                        'multi-sub2-sub3' => array(
                            'value' => 'value multi-sub2'
                        )
                    )
                ),
                array(
                    'value' => 'value multi-sub2'
                ),
            ),
        ),
    ),
    'duplicateElement' => array(
        array(
            '@attributes' => array(
                'atDuplicate1' => 'val'
            ),
            'value' => 'valueDuplicateElement1',
        ),
        array(
            '@attributes' => array(
                'atDuplicate2' => 'val'
            ),
            'value' => 'valueDuplicateElement2',
        ),
        array(
            '@attributes' => array(
                'atDuplicate3' => 'val'
            ),
            'value' => array(
                '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 = array(
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => array(
        '@attributes' => array(
            'at1' => 'at1val',
            'at2' => 'at2val',
        ),
        'value' => 'this is long text',
    ),
);

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

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

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