https://github.com/sausin/xml-construct
Generate valid XML using array inputs in PHP
https://github.com/sausin/xml-construct
Last synced: 8 months ago
JSON representation
Generate valid XML using array inputs in PHP
- Host: GitHub
- URL: https://github.com/sausin/xml-construct
- Owner: sausin
- License: mit
- Created: 2017-09-09T11:12:09.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-10T21:33:07.000Z (about 8 years ago)
- Last Synced: 2024-03-17T18:31:49.652Z (over 1 year ago)
- Language: PHP
- Size: 11.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Xml generator
[](https://packagist.org/packages/sausin/xml-construct)
[](https://travis-ci.org/sausin/xml-construct)
[](https://scrutinizer-ci.com/g/sausin/xml-construct)
[](https://scrutinizer-ci.com/g/sausin/xml-construct)
[](https://styleci.io/repos/102949349)
[](https://packagist.org/packages/sausin/xml-construct)
[](https://opensource.org/licenses/MIT)A useful class to generate valid XML from a PHP array.
## Installation
Run the following command in your project to get the class:
```
composer require sausin/xml-construct
```## Usage with normal arrays
Usage is simple
### Verbose way:
```php
$xmlGen = new XmlConstruct('ROOT')$string = $xmlGen->fromArray($array)->getDocument();
```
where `$array` is the PHP array from which you need the XML to be generated.### Quick:
```php
(new XmlConstruct('ROOT'))->fromArray($f)->getDocument();
```
returns the XML string.In both the above examples, `ROOT` is the root of the XML (i.e. the first element).
## Usage with arrays when attributes are needed in XML
If used like this:
```
$array = ['KEY|ATTR|VAL' => 'VALUE'];return (new XmlConstruct('ROOT'))->fromArray($f)->getDocument();
```It will result in the following XML
```xmlVALUE
```
You can add as many attributes as you like and they will all be added to the element. Neat!
## Credits
Initial inputs to the class were taken from [php user contributed notes](http://php.net/manual/en/ref.xmlwriter.php)