alextartan/xml2array

Lightweight xml<->array conversion.

2.0.2 2020-11-09 09:38 UTC

README

Array <-> XML conversion package

Lightweight XML parser

Current build status

Build Status codecov Infection MSI Dependabot Status Downloads

Install

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

Usage

ArrayToXml

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);
XmlToArray

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);

Issues and pull requests.

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.