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.
You can install the package via composer:
composer require dmb/xml-converter
Below some examples of use.
$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',
],
]
);
<?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>
Below some examples of use.
The xml will be converted to array.
try {
$converted = (new FromXml())
->convertToArray($validXml);
} catch (XmlParsingExcpetion $e) {
$error = $e->getMessage();
}
An XmlParsingExcpetion will be caught.
try {
$converted = (new FromXml())
->convertToArray($invalidXML);
} catch (XmlParsingExcpetion $e) {
$error = $e->getMessage();
}
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.