PHP code example of cosmin-ciolacu / simple-xml-to-array

1. Go to this page and download the library: Download cosmin-ciolacu/simple-xml-to-array 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/ */

    

cosmin-ciolacu / simple-xml-to-array example snippets


use CosminCiolacu\SimpleXmlToArray\SimpleXmlToArray;

$xml = '<root><item>value</item></root>';

$array = SimpleXmlToArray::convert($xml);

print_r($array);

use CosminCiolacu\SimpleXmlToArray\Exceptions\InvalidXmlException;
use CosminCiolacu\SimpleXmlToArray\SimpleXmlToArray;

$invalidXml = '<root><item>value</item>';

try {
    $array = SimpleXmlToArray::convert($invalidXml);
} catch (InvalidXmlException $e) {
    echo $e->getMessage();
}

use CosminCiolacu\SimpleXmlToArray\SimpleXmlToArray;
use CosminCiolacu\SimpleXmlToArray\Exceptions\InvalidXmlException;

try {
    $xmlFile = 'path/to/file.xml';
    $data = SimpleXmlToArray::convert($xmlFile, "file");

    print_r($data);
} catch (InvalidXmlException $e) {
    echo $e->getMessage();
}