sausin/xml-construct

This package is abandoned and no longer maintained. The author suggests using the spatie/array-to-xml package instead.

XML constructor class for PHP

v0.2.0 2017-09-09 20:50 UTC

This package is auto-updated.

Last update: 2022-02-01 13:09:39 UTC


README

Latest Version on Packagist Build Status Quality Score Scrutinizer Coverage StyleCI Total Downloads License: 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:

$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:

(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

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
  <KEY ATTR="VAL">VALUE</KEY>
<ROOT>

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