Download the PHP package susina/xml-to-array without Composer
On this page you can find all versions of the php package susina/xml-to-array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download susina/xml-to-array
More information about susina/xml-to-array
Files in susina/xml-to-array
Package xml-to-array
Short Description Simple PHP library to convert an XML string into an array
License Apache-2.0
Informations about the package xml-to-array
Xml to Array Converter
Xml to Array is a simple library to convert XML into PHP array.
The library consists in one namespace Susina\XmlToArray
and two classes:
Converter
: to convert an XML string into PHP arrayFileConverter
: to convert an XML file
Both classes expose the same public api:
- Susina\XmlToArray\Converter
convert(string $xmlToParse): array
to convert an xml string into an arrayconvertAndSave(string $xmlToParse, string $filename): void
to convert an xml string to an array and save it into a regular php file.
- Susina\XmlToArray\FileConverter
convert(string $xmlFile): array
to read an xml file and convert it into an arrayconvertAndSave(string $xmlFile, string $filename): void
to read an xml file and, convert it into an array and save it into a regular php file.
Installation
Install the library via composer:
The library depends on three php extensions, usually installed by default:
- libxml
- simplexml
- dom
and Symfony Options Resolver component, that'll be install by composer.
Usage
Use the convert
method to parse an XML string. Let's get a look at the following example:
Alternatively, you can use the static instantiator:
If you want to read and convert an xml file, you can play with FileConverter
class:
and by using the static constructor:
You can save the converted array into a regular formatted php file, that you can import in some other script by include PHP statement.
For example:
The content of array_file.php
is the following:
You can load your array via include
statement:
Also FileConverter
class has its convertAndSave
method, which has the same behavior, but it accepts the name of the xml file to convert as first parameter:
Configuration
You can configure the converters by passing an associative array to the constructor, where the keys are the name of the option. The available options are the following:
- mergeAttributes: boolean, default true
- typesAsString: boolean, default false
- preserveFirstTag: boolean, default false
mergeAttributes
Default: true
When this option is set to true, the attributes of a tag are merged into the tag it self, otherwise they're saved into a @attribute
array, i.e.:
In the previous example, you can see that the name attribute is "merged" into logger array.
When this option is set to false, a @attribute array is created:
typesAsString
Default: false
The normal behavior of this library is to preserve all PHP types (boolean, numeric, null etc.). If typesAsString option is set to true all the values are considered strings:
In the previous example, if you set the property to true, all values are strings:
preserveFirstTag
Default: false
If your xml document starts with a single tag, containing all the others, the first tag is not considered as part of the resulting array:
If you want to keep this tag as the first key of your array, set this option to true:
Issues
If you find a bug or any other issue, please report it on Github.
Contributing
Please, see CONTRIBUTING.md
Licensing
This library is released under Apache-2.0 license.
All versions of xml-to-array with dependencies
ext-libxml Version *
ext-simplexml Version *
ext-dom Version *
symfony/options-resolver Version ^7.1