PHP code example of susina / xml-to-array

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

    

susina / xml-to-array example snippets


 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = "
<?xml version='1.0' standalone='yes'

 declare(strict_types=1);

.....

$array = Converter::create()->convert($xmlString);


 declare(strict_types=1);

use Susina\XmlToArray\FileConverter;

$converter = new FileConverter();
$array = $converter->convert('/my_dir/my_file.xml');

 declare(strict_types=1);

$array = FileConverter::create()->convert('/my_dir/my_file.xml');

 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = "
<?xml version='1.0' standalone='yes'

 declare(strict_types=1);
/*
 * This file is auto-generated by susina/xml-to-array library.
 */

return array (
  'movie' => 
  array (
    0 => 
    array (
      'title' => 'Star Wars',
      'starred' => true,
      'percentage' => 32.5,
    ),
    1 => 
    array (
      'title' => 'The Lord Of The Rings',
      'starred' => false,
      'percentage' => 30.7,
    ),
  ),
);

 declare(strict_types=1);

//Some instructions

$array = 

 declare(strict_types=1);
...........

FileConverter::create()->convertAndSave($xmlFileName, 'array_file.php');

 declare(strict_types=1);

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

 declare(strict_types=1);

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

 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = "
<?xml version='1.0' standalone='yes'

 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = .......

$array = Converter::create(['typesAsString' => true])->convert($xmlString);

/*
 * $array now contains the following array:
 * 
 * [
 *     "movie" => [
 *         0 => [
 *             "title"      => "Star Wars",
 *             "starred"    => 'True',
 *             "percentage" => '32.5'
 *             "views"      => '589623'
 *         ],
 *     ]
 * ]
 */

 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = "
<?xml version='1.0' standalone='yes'

 declare(strict_types=1);

use Susina\XmlToArray\Converter;

$xmlString = .............

$converter = new Converter(['preserveFirstTag' => true]);
$array = $converter->convert($xmlString);

/*
 * $array now contains the following array:
 * 
 * [
 *     "database => [
 *         "movie" => [
 *             0 => [
 *                 "title" => "Star Wars",
 *             ],
 *             1 => [
 *                 "title" => "The Lord Of The Rings",
 *             ],
 *             2 => [
 *                 "title" "Spider-Man" 
 *             ]
 *         ]
 *     ]
 * ]
 */