PHPackages                             jolti/dtotoxml - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. jolti/dtotoxml

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

jolti/dtotoxml
==============

A PHP library for serializing and unserializing objects, including those with subtypes, into XML documents.

1.0.0(1y ago)13PHP

Since Jan 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/mohamedjolti/dto2xml)[ Packagist](https://packagist.org/packages/jolti/dtotoxml)[ RSS](/packages/jolti-dtotoxml/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

XML Serializer Package
======================

[](#xml-serializer-package)

This package provides a simple and efficient way to serialize DTOs (Data Transfer Objects) with subtypes to XML and deserialize XML back to PHP objects.

What is `dto2xml`?
------------------

[](#what-is-dto2xml)

At its core, `dto2xml` is a PHP library that lets you:

- Serialize objects (including those with subtypes) into well-structured XML documents.
- Unserialize XML back into PHP objects effortlessly.

The library is built with flexibility and ease of use in mind, making it ideal for both small-scale and enterprise-level applications.

---

Why `dto2xml`?
--------------

[](#why-dto2xml)

Here are some reasons to consider `dto2xml` for your next project:

1. **Handles Subtypes Gracefully**: Many libraries struggle when dealing with objects containing subtypes or nested structures. `dto2xml` shines in this area.
2. **Configurable Output**: You can customize XML headers, namespaces, and more to match your specific requirements.
3. **Clean and Intuitive API**: With a few lines of code, you can transform objects into XML or parse XML back into objects.

---

Installation
------------

[](#installation)

To get started with `dto2xml`, simply install it via Composer:

```
composer require jolti/dto2xml
```

---

How to Use `dto2xml`
--------------------

[](#how-to-use-dto2xml)

### Setting Up the Serializer

[](#setting-up-the-serializer)

Let’s begin by setting up the serializer with a custom configuration:

```
use Dtotoxml\XmlSerializer;
use Dtotoxml\Configuration;
use Samples\Dto\School;
use Samples\Dto\SchoolFixtures;

$xmlSerialize = new XmlSerializer();

// Configure the serializer
$config = new Configuration();
$config->setHead('');
$config->setNameSpaceOutput("Samples\\Dto");
$xmlSerialize->setConfig($config);
```

### Serializing an Object to XML

[](#serializing-an-object-to-xml)

To serialize an object, such as a `School` object with nested attributes, follow this approach:

```
$school = SchoolFixtures::createSchool();
$xml = $xmlSerialize->format($xmlSerialize->serialise($school));

// Output the XML
echo '', htmlentities($xml), '';
```

### Deserializing XML to an Object

[](#deserializing-xml-to-an-object)

Converting XML back into a PHP object is just as easy:

```
$xmlString = SchoolFixtures::createSchoolXml();
$object = $xmlSerialize->unserialise($xmlString, School::class);

// Access the deserialized object
var_dump($object);
```

### Example DTO: School

[](#example-dto-school)

Here’s a sample Data Transfer Object (DTO) for a school, complete with subtypes:

```
namespace Samples\Dto;

class School
{
    private $name;
    private $adresse;
    private $teachers;
    private $rooms;

    public function getAttributes(): array
    {
        return ["name"];
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getAdresse(): Adresse
    {
        return $this->adresse;
    }

    public function setAdresse(Adresse $adresse): void
    {
        $this->adresse = $adresse;
    }

    public function getTeachers(): array
    {
        return $this->teachers;
    }

    public function setTeachers(array $teachers): void
    {
        $this->teachers = $teachers;
    }

    public function getRooms(): array
    {
        return $this->rooms;
    }

    public function setRooms(array $rooms): void
    {
        $this->rooms = $rooms;
    }
}
```

---

Annotations for Serialization and Deserialization
-------------------------------------------------

[](#annotations-for-serialization-and-deserialization)

The `dto2xml` library uses annotations to guide the serialization and deserialization process, providing powerful customization options. Here are the key annotations:

### **`@isAttribute`**

[](#isattribute)

- **Purpose**: Marks a property as an XML attribute instead of an XML element.
- **Example**: ```
    /**
     * @isAttribute
     * @var string $id
     */
    private string $id;
    ```

    - The `id` field will be serialized as an attribute of the parent XML element, not as a child element.
    - **XML Example**: ```

        ```
    - This is useful for compact XML representations where certain values are better represented as attributes.

---

### **`@outputName`**

[](#outputname)

- **Purpose**: Specifies the name of the field when serializing the object into XML.
- **Example**: ```
    /**
     * @outputName adresse-city
     * @inputName adresse-city
     * @var string $city
     */
    private $city;
    ```

    - The `city` property will appear as `` in the XML output.
    - **XML Example**: ```

            Tangier

        ```
    - This allows you to control the naming conventions in your XML output to match specific schemas or external requirements.

---

### **`@inputName`**

[](#inputname)

- **Purpose**: Specifies the name of the field when deserializing XML back into a PHP object.
- **Example**: ```
    /**
     * @outputName adresse-city
     * @inputName adresse-city
     * @var string $city
     */
    private $city;
    ```

    - During deserialization, the library will map the `` XML element back to the `city` property in the `Adresse` object.
    - **XML Example**: ```

            Tangier

        ```
    - This ensures that even if the XML uses custom or non-standard names, the library can correctly map them to the appropriate object properties.

---

### Practical Usage

[](#practical-usage)

These annotations are critical for ensuring flexibility in how the `dto2xml` library handles XML. They allow you to:

1. **Customize XML Output**:
    - Use `@outputName` to control the tag names for fields, ensuring compatibility with external systems.
2. **Handle Non-Standard XML Inputs**:
    - Use `@inputName` to map non-standard XML names to your PHP properties.
3. **Compact Representations**:
    - Use `@isAttribute` to define compact XML structures by leveraging attributes instead of child elements.

---

Configuration Options
---------------------

[](#configuration-options)

The `Configuration` class allows you to customize the serialization process to fit your needs. For example:

- **XML Header**: Set a custom XML declaration using `$config->setHead()`.
- **Namespace**: Define the namespace for your XML output with `$config->setNameSpaceOutput()`.

---

Why Developers Love `dto2xml`
-----------------------------

[](#why-developers-love-dto2xml)

1. **Ease of Use**: The library’s API is straightforward and developer-friendly.
2. **Powerful Features**: It handles nested objects and subtypes with ease.
3. **Customizability**: You can tweak the output XML to meet any specification.

---

Get Started Today!
------------------

[](#get-started-today)

Ready to simplify your XML serialization tasks? Download `dto2xml` today and experience its power firsthand.

Feel free to contribute to the project, report issues, or suggest features. Let’s make XML handling in PHP simpler, together.

---

Stay Connected
--------------

[](#stay-connected)

Follow us for updates and tips on getting the most out of `dto2xml`. Happy coding!

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance44

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

476d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c1986aa22bf3e65e86c9b9afe3c3359a48bc820bef4ee65276ad14b19c0d5060?d=identicon)[mohamed\_jolti](/maintainers/mohamed_jolti)

---

Top Contributors

[![mohamedjolti](https://avatars.githubusercontent.com/u/45952923?v=4)](https://github.com/mohamedjolti "mohamedjolti (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jolti-dtotoxml/health.svg)

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M283](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)[michelf/php-markdown

PHP Markdown

3.5k52.4M344](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)

PHPackages © 2026

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