bulton-fr/php-to-xml

Convert php structure to xml file

1.0.0 2016-10-23 00:00 UTC

This package is auto-updated.

Last update: 2024-11-28 23:06:18 UTC


README

Convert php basic structure to xml document

Build Status Coverage Status Scrutinizer Code Quality

This library is to convert a basic php structure to xml document. Xml attributes is not implemented. The main goal is to convert a php structure to json or xml.

Install

With composer: curl -sS https://getcomposer.org/installer | php

Add in your composer.json

{
    "require": {
        "bulton-fr/php-to-xml": "@stable"
    }
}

## Example

PHP:

$phpStructure = (object) [
	'elements' => (object) [
		'elemA' => [
			0 => (object) [
				'elemB' => 'Toto',
				'elemC' => 'Foo',
				'elemD' => (object) [
					'elemE' => [
						'Foo',
						'Bar'
					]
				]
			],
			1 => (object) [
				'elemB' => 'Titi',
				'elemC' => 'Fii',
				'elemD' => (object) [
					'elemE' => [
						'Fii',
						'Ber'
					]
				]
			]
		]
	]
];

echo json_encode($phpStructure);

$phpToXml = new \bultonFr\PhpToXml\PhpToXml;
echo $phpToXml->convert($phpStructure);

JSON:

{
    "elements":{
        "elemA":[
            {
                "elemB":"Toto",
                "elemC":"Foo",
                "elemD":{
                    "elemE":[
                        "Foo",
                        "Bar"
                    ]
                }
            },
            {
                "elemB":"Titi",
                "elemC":"Fii",
                "elemD":{
                    "elemE":[
                        "Fii",
                        "Ber"
                    ]
                }
            }
        ]
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<elements>
    <elemA>
        <elemB>Toto</elemB>
        <elemC>Foo</elemC>
        <elemD>
            <elemE>Foo</elemE>
            <elemE>Bar</elemE>
        </elemD>
    </elemA>
    <elemA>
        <elemB>Titi</elemB>
        <elemC>Fii</elemC>
        <elemD>
            <elemE>Fii</elemE>
            <elemE>Ber</elemE>
        </elemD>
    </elemA>
</elements>