Simple class to convert PHP objects to XML, with tests.

v1.0.0 2019-08-03 21:55 UTC

This package is auto-updated.

Last update: 2023-03-04 05:37:46 UTC


Latest Stable Version License Build Status

Just a simple class that converts a PHP object to XML, with tests, and no SimpleXML requirement.

Yes, I'm fully aware of the existence of SimpleXMLElement::asXML() and solutions that use it.


First include the package in your project via composer.

composer require salernolabs/php-to-xml

An example usage would be something like this:

$object = new \stdClass();
$object->hello = 'world';
$object->items = ['one', 'two', 'three'];
$object->samples = ['sample1'=>true, 'sample2'=>false, 'sample3'=>'I dunno!'];

$converter = new \SalernoLabs\PHPToXML\Convert();
$xml = $converter

At this point, the value of $xml is expected to be a string with the following contents:

<?xml version="1.0" encoding="utf-8"?>
        <sample3>I dunno!</sample3>

The converter is currently setup so multiple calls to convert() will return the last created data. Calls to setObjectData will clear the cache.


I haven't added attribute or CDATA support in yet. It also doesn't collapse/remove empty nodes. Maybe later, pull requests welcome!