Lightweight XML parser
The easiest way is to use composer
:
composer require alextartan/xml2array
Notes:
Latest release requires PHP
>= 7.2 and the dom
extension (ext-dom
)
For PHP
<= 7.2, use version 1.0.2
Convert an XML (either DOMDocument or string) to an array
// default value:
$config = [
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'useNamespaces' => false,
'forceOneElementArray => false,
];
$xtoa = new XmlToArray($config);
$array = $xtoa->buildArrayFromString($xmlString);
$array = $xtoa->buildArrayFromDomDocument($xmlDom);
Convert an array to a DOMDocument
// default value:
$config = [
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'formatOutput' => false,
];
$atox = new ArrayToXml($config);
$xml = $atox->buildXml($array);
Any issues found should be reported in this repository issue tracker, issues will be fixed when possible. Pull requests will be accepted, but please adhere to the PSR2 coding standard. All builds must pass in order to merge the PR.