XML adapter for Nette Dependency Injection Component

v2.0.0 2020-03-31 08:54 UTC

This package is not auto-updated.

Last update: 2024-02-14 04:08:16 UTC


Build Status

XML adapter could be used to write configuration in XML. Thanks to namespace usage ( you can have configuration common for your php application (nette) and other application in the same xml file.

XmlAdapter reads xml file and convert it to PHP array.

See xml example here: tests/Adapters/files/xmlAdapter.xml

Supported types

Associative array



[key1 => 'value1', key2=>'value2']

Numeric array

<myarray array="numeric"><key>value1</key><key>value2</key></myarray>

Becomes (element names are ignored here):


String, Null, Number, Boolean

<myarray array="numeric">
<x> trimmed string </x>
<x space="preserve"> string with spaces  </x>
<x number="2"/>
<x bool="yes"/>
<x null="null" />


["trimmed string", " string with spaces  ","1", 2, TRUE, NULL]

Bool support "yes", "true", "on" and "1"


<factory statement="statement">
    <s><ent>DateTime</ent><args><a numeric="0" /></args></s>

Is equivalent to neon: DateTime(0)::format("%B")

Syntactic sugar

Array from string

<myarray array="string">1,2,3</myarray>


["1", "2", "3"]

The same with custom delimiter:

<myarray array="string" delimiter=";">1;2;3</myarray>


Of course you can use array from string as statement arguments.

<xxx statement="statement">
<s><ent>fooo</ent><args array="string" delimiter=";">1;2;3</args></s>

If there is only one argument, you dont need to use element inside args element. These statements are equivalent:

<xxx statement="statement"><s><ent>fooo</ent><args><a>1</a></args></s></xxx>
<xxx statement="statement"><s><ent>fooo</ent><args>1</args></s></xxx>

You can use neon syntax for statement:

<xxx statement="DateTime(0)::format('%B')" />


Install with composer

composer require sallyx/nette-xml-adapter


require 'vendor/autoload.php';
use Sallyx\Nette\DI\Config\Adapters\XmlAdapter;

$adapter = new XmlAdapter;
$file = 'vendor/sallyx/nette-xml-adapter/tests/Adapters/files/xmlAdapter.xml';
$config = $adapter->load($file);

Usage with Nette

Add this line into your app/bootstrap.php after $configurator = new Nette\Configurator;

$xmlAdapter = new Sallyx\Nette\DI\Config\Adapters\XmlAdapter;
$configurator->addAdapter('xml', $xmlAdapter);

And now you can load XML configuration files (alongside with neon files). For example:

$configurator->addConfig(__DIR__ . '/config/config.xml', Nette\Config\Configurator::AUTO);

How to convert neon config file to xml config file

require __DIR__ . '/vendor/autoload.php';
use Nette\DI\Config\Adapters\NeonAdapter;
use Sallyx\Nette\DI\Config\Adapters\XmlAdapter;

$na = new NeonAdapter;
$xa = new XmlAdapter;
$config = $na->load('config.neon');
$xmlConfig = $xa->dump($config);
// pretty output
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
echo $domxml->saveXML();