Skip to content

A package that allows you to easily convert your XML string into array format or generate an XML string from an array.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

davidemariabusi/xml-converter

Repository files navigation

Xml Converter - convert from xml to array and viceversa

Latest Version on Packagist

A package that allows you to easily convert your XML string into array format or generate an XML string from an array. This package is based on: Spatie Array to XML and Gaarf XML string to PHP array.


Installation

You can install the package via composer:

composer require dmb/xml-converter

From Array to XML


Below some examples of use.

Default root name: root

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

Custom root name: customRootName

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

Custom root name: customRootName and attributes

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

Example with all features

$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',
            ],
        ]
    );

Result

<?xml version="1.0"?>
<customRootName xmlns="https://github.com/davidemariabusi/xml-converter">
    <First_User attr1="value">
        <name>Name</name>
        <weapon>Lightsaber</weapon>
    </First User>
    <Second_User>
        <name>Name2</name>
    </Second_User>
    <Third_User attr2="value 2">
        Name 3
    </Third_User>
</customRootName>

From XML to Array


Below some examples of use.

With valid XML

The xml will be converted to array.

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

With invalid XML

An XmlParsingExcpetion will be caught.

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

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

A package that allows you to easily convert your XML string into array format or generate an XML string from an array.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Languages