PHP code example of dmb / xml-converter

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

    

dmb / xml-converter example snippets


$arrayConverted = (new FromArray())
    ->convertToXml($arrayToConvert);

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        'customRootName'
    );

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        [
        'rootElementName' => 'customRootName',
        '_attributes' => [
            'xmlns' => 'https://github.com/davidemariabusi/xml-converter',
        ],
    );

$arrayToConvert = [
    'First_User' => [
        '_attributes' => [
            'attr1' => 'value'
        ],
        'name' => 'Name',
    ],
    'Second_User' => [
        'name' => 'Name 2'
    ],
    'Third_User' => [
        '_attributes' => [
            'attr2' => 'value 2'
        ],
        '_value' => 'Name 3'
    ]
];

$arrayConverted = (new FromArray())
    ->convertToXml(
        $arrayToConvert,
        [
            'rootElementName' => 'customRootName',
            '_attributes' => [
                'xmlns' => 'https://github.com/davidemariabusi/xml-converter',
            ],
        ]
    );

try {
    $converted = (new FromXml())
        ->convertToArray($validXml);
} catch (XmlParsingExcpetion $e) {
    $error = $e->getMessage();
}

try {
    $converted = (new FromXml())
        ->convertToArray($invalidXML);
} catch (XmlParsingExcpetion $e) {
    $error = $e->getMessage();
}