PHPackages                             magicsunday/xmlmapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. magicsunday/xmlmapper

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

magicsunday/xmlmapper
=====================

Map PHP to XML

3.0.1(1mo ago)01.7k↓73.5%2[7 issues](https://github.com/magicsunday/xmlmapper/issues)[1 PRs](https://github.com/magicsunday/xmlmapper/pulls)1MITPHPPHP ^8.3CI passing

Since Jan 25Pushed 2w ago1 watchersCompare

[ Source](https://github.com/magicsunday/xmlmapper)[ Packagist](https://packagist.org/packages/magicsunday/xmlmapper)[ Fund](https://paypal.me/magicsunday)[ RSS](/packages/magicsunday-xmlmapper/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (7)Dependencies (39)Versions (15)Used By (1)

XmlMapper: PHP Object to XML Mapping
====================================

[](#xmlmapper-php-object-to-xml-mapping)

 Map strongly-typed PHP objects to XML using Symfony's PropertyInfo and TypeInfo components.

 [![CI](https://github.com/magicsunday/xmlmapper/actions/workflows/ci.yml/badge.svg)](https://github.com/magicsunday/xmlmapper/actions/workflows/ci.yml)

 [![PHPStan Max Level](https://camo.githubusercontent.com/ecb39a33957e802f1f085f1debada1e99904e72b8d807e98991fb7f9660cb6d3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6d61782532306c6576656c2d627269676874677265656e2e737667)](https://phpstan.org/) [![PHPUnit 12 | 13](https://camo.githubusercontent.com/bd42f2ee7679d1b9d573bd29bc8877183e527fbdda4aea6af5ce24b007e7a913/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f504850556e69742d313225323025374325323031332d626c75652e737667)](https://phpunit.de/) [![Rector 2.0](https://camo.githubusercontent.com/9e64e770b8b919b97683d84fefdff21986835b3c8e9afbe5f5e23c9668668315/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f526563746f722d322e302d6f72616e67652e737667)](https://getrector.com/) [![PER-CS 2.0](https://camo.githubusercontent.com/71375eab3fd6f5e7d3580e6397b4f0058e9cbd4bc38980314320c7678006ea2e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f64652532305374796c652d5045522d2d4353253230322e302d626c75652e737667)](https://www.php-fig.org/per/coding-style/)

 [![PHP Version](https://camo.githubusercontent.com/43d6e88f632bb51600940462d06eaf427de49ff9156e7eaecbea06fbc1243011/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d382e33253230253743253230382e34253230253743253230382e352d626c7565)](composer.json)

 [![Latest version](https://camo.githubusercontent.com/5d199835fa40c503287a36b12e7fb570fc9f6921fe1c52b03450ef43e43c0942/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d6167696373756e6461792f786d6c6d61707065723f736f72743d73656d766572)](https://github.com/magicsunday/xmlmapper/releases/latest) [![License](https://camo.githubusercontent.com/22e17567a933204f65c9964b5577434bfcd5e383d69b480bbe452d838085314b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6167696373756e6461792f786d6c6d6170706572)](LICENSE)

---

📌 Overview
----------

[](#-overview)

XmlMapper is a PHP library that maps strongly-typed PHP objects (DTOs, value objects, entities) to XML using reflection and PHPDoc annotations. It leverages Symfony's PropertyInfo and TypeInfo components to derive each property's type and render a matching XML document.

KeyValuePackage`magicsunday/xmlmapper`PHP`^8.3`Main API`MagicSunday\XmlEncoder`OutputXML string (`string`); `false` only if serialization itself fails❓ What is this?
---------------

[](#-what-is-this)

XmlMapper takes a PHP object implementing `MagicSunday\XmlSerializable` and renders it as XML, including nested objects, scalar and object collections, and custom types. Property values are routed to elements, attributes, raw text nodes or CDATA sections via a small set of annotations, and property names can be converted on the fly (e.g. snake\_case to camelCase).

🎯 Why does this exist?
----------------------

[](#-why-does-this-exist)

Serializing domain objects to XML usually means hand-writing `DOMDocument`/`XMLWriter` boilerplate that drifts from the underlying model. XmlMapper derives the XML structure from the object's typed properties and a few annotations, so the output follows the PHP model, with explicit hooks (attributes, CDATA, node values, custom type closures) where you need to deviate.

🚀 Usage
-------

[](#-usage)

```
composer require magicsunday/xmlmapper
```

### Quick start

[](#quick-start)

Annotate the classes you want to serialize and let them implement `XmlSerializable`:

```
namespace App\Model;

use MagicSunday\XmlSerializable;
use MagicSunday\XmlMapper\Annotation\XmlAttribute;

final class Author implements XmlSerializable
{
    public string $name = 'Jane Doe';
}

final class Book implements XmlSerializable
{
    #[XmlAttribute]
    public string $isbn = '978-3-16-148410-0';

    public string $title = 'The Title';

    public Author $author;

    /**
     * @var string[]
     */
    public array $tags = ['php', 'xml'];
}
```

Build an encoder and map an instance:

```
require __DIR__ . '/vendor/autoload.php';

use App\Model\Author;
use App\Model\Book;
use MagicSunday\XmlEncoder;
use MagicSunday\XmlMapper\Converter\CamelCasePropertyNameConverter;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

$extractor = new PropertyInfoExtractor(
    [new ReflectionExtractor()],
    // PhpDocExtractor resolves `@var` generics such as `Chapter[]`;
    // ReflectionExtractor covers native types, so an array property without a
    // docblock is still recognised as a collection instead of being silently
    // rendered as one empty element.
    [new PhpDocExtractor(), new ReflectionExtractor()]
);

$book         = new Book();
$book->author = new Author();

$encoder = new XmlEncoder($extractor, new CamelCasePropertyNameConverter());

echo $encoder->map($book);
```

```

  The Title

    Jane Doe

  php
  xml

```

The name converter is optional; without it the raw class and property names are used verbatim. For collections, annotate the property with the phpDocumentor collection type so the value type can be resolved:

```
/** @var string[] $tags */
/** @var Chapter[] $chapters */
/** @var array $members */
```

### Property markers

[](#property-markers)

Each marker is applied as a native PHP attribute (shown above).

MarkerEffect`XmlAttribute`Render the value as an attribute of the surrounding element.`XmlNodeValue`Render the value as the raw text content of the surrounding element.`XmlCDataSection`Wrap the value in a `` section (markup left intact).### Custom types

[](#custom-types)

Register a closure to transform every value of a given class or builtin type (`Money::class`, `bool`, `int`, `array`, `object`, …) before it is written:

```
$encoder->addType('bool', static fn (string $name, mixed $value): string => $value === true ? 'yes' : 'no');
```

📚 Documentation
---------------

[](#-documentation)

- [API reference](docs/API.md)
- Recipes
    - [Manual instantiation](docs/recipes/manual-instantiation.md) — wiring the Symfony extractor and name converter
    - [Markers: attributes, node values and CDATA](docs/recipes/markers.md) — native attribute syntax
    - [Custom types](docs/recipes/type-converters.md) — transforming values with `addType()`
    - [Custom name converter](docs/recipes/custom-name-converter.md) — element naming
    - [Collections](docs/recipes/collections.md) — scalar, object, nullable and union-typed collections

🛠️ Development
--------------

[](#️-development)

Prerequisites:

- PHP `^8.3`
- Extensions: `dom`, `xml`
- Node.js (for the copy-paste detection gate, run via `npx`)

Install dependencies:

```
composer install
```

Run the mandatory quality gate:

```
composer ci:test
```

`ci:test` includes:

- Linting (`phplint`)
- Unit tests (`phpunit`)
- Static analysis (`phpstan`, max level)
- Refactoring dry-run (`rector --dry-run`)
- Coding standards dry-run (`php-cs-fixer --dry-run`)
- Copy-paste detection (`jscpd`)

🤝 Contributing
--------------

[](#-contributing)

Contributions are welcome. Please run the full `composer ci:test` quality gate before submitting a pull request, and keep changes covered by tests.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance95

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 92.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~128 days

Total

8

Last Release

31d ago

Major Versions

1.2.0 → 2.0.02026-06-12

2.x-dev → 3.0.02026-06-12

PHP version history (5 changes)1.0.0PHP ^8.1

1.0.1PHP &gt;=8.1.0 &lt;8.4.0

1.2.0PHP &gt;=8.2.0 &lt;8.5.0

2.0.0PHP ^8.2

3.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/1979dde7200fccc0e21e18a29b5566f22fa01ad104e577254fe74e14ae04a297?d=identicon)[magicsunday](/maintainers/magicsunday)

---

Top Contributors

[![magicsunday](https://avatars.githubusercontent.com/u/564393?v=4)](https://github.com/magicsunday "magicsunday (71 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![CybotTM](https://avatars.githubusercontent.com/u/326348?v=4)](https://github.com/CybotTM "CybotTM (2 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/magicsunday-xmlmapper/health.svg)

```
[![Health](https://phpackages.com/badges/magicsunday-xmlmapper/health.svg)](https://phpackages.com/packages/magicsunday-xmlmapper)
```

###  Alternatives

[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k52.2M366](/packages/api-platform-core)[api-platform/metadata

API Resource-oriented metadata attributes and factories

275.5M252](/packages/api-platform-metadata)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k6.0M766](/packages/sylius-sylius)[pimcore/pimcore

Content &amp; Product Management Framework (CMS/PIM/E-Commerce)

3.8k3.9M532](/packages/pimcore-pimcore)[shopware/platform

The Shopware e-commerce core

3.4k1.5M3](/packages/shopware-platform)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.8M651](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
