PHPackages                             dmb/xml-converter - 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. dmb/xml-converter

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

dmb/xml-converter
=================

A package that allows you to easily convert your XML string into array format or generate an XML string from an array.

v0.1.4(3mo ago)03231MITPHPPHP ^8.2

Since Jan 20Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/davidemariabusi/xml-converter)[ Packagist](https://packagist.org/packages/dmb/xml-converter)[ Docs](https://github.com/davidemariabusi/xml-converter)[ RSS](/packages/dmb-xml-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (7)Used By (0)

XML Converter
=============

[](#xml-converter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c3d2f5abc2b54d4cd85474f5d40c785b62c12775e1b4518358180bab41dc292e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646d622f786d6c2d636f6e7665727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dmb/xml-converter)

Convert XML to array, convert array to XML, and navigate parsed XML with a fluent API.

Requirements
------------

[](#requirements)

- PHP `^8.2`

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

[](#installation)

```
composer require dmb/xml-converter
```

What this package does
----------------------

[](#what-this-package-does)

- **`FromXml`**: converts an XML string into a normalized array structure.
- **`FromArray`**: converts a compatible array structure into XML.
- **`Fluent`**: provides safe, chainable navigation and collection-style helpers on parsed XML arrays.

Array structure used by the package
-----------------------------------

[](#array-structure-used-by-the-package)

The package uses a consistent structure:

- `'_attributes'` for XML attributes
- `'_value'` for text content
- `'_children'` for ordered/repeated child nodes

Example:

```
[
    'ParentTag' => [
        '_attributes' => [
            'Version' => '1.0',
        ],
        '_children' => [
            [
                'Success' => [
                    '_attributes' => ['Version' => '1.4'],
                    '_children' => [
                        ['Foo' => []],
                    ],
                ],
            ],
        ],
    ],
]
```

Usage
-----

[](#usage)

### 1) XML to array

[](#1-xml-to-array)

```
use Dmb\XmlConverter\FromXml;
use Dmb\XmlConverter\XmlParsingException;

$xml =  [
        'version' => [
            '_attributes' => ['port' => '0000', 'host' => 'host'],
            '_value' => '1.0.0',
        ],
        'timestamp' => '20230116170354',
    ],
    'response' => [
        '_attributes' => ['type' => 'type', 'product' => 'item'],
        '_children' => [
            ['search' => ['_attributes' => ['number' => '123', 'time' => '0.00']]],
            ['nights' => ['_attributes' => ['number' => '11']]],
        ],
    ],
];

// Default root element (library default)
$xmlDefaultRoot = FromArray::make()->convertToXml($payload);

// Custom root element
$xmlCustomRoot = FromArray::make()->convertToXml($payload, 'envelope');

// Custom root element with attributes
$xmlCustomRootWithAttributes = FromArray::make()->convertToXml(
    $payload,
    [
        'rootElementName' => 'envelope',
        '_attributes' => [
            'xmlns' => 'https://github.com/davidemariabusi/xml-converter',
        ],
    ]
);
```

### 3) Fluent navigation API

[](#3-fluent-navigation-api)

```
use Dmb\XmlConverter\Fluent;
use Dmb\XmlConverter\FluentException;

$data = [
    'SOAP-ENV:Envelope' => [
        '_attributes' => ['Version' => '1.0'],
        '_children' => [
            [
                'SOAP-ENV:Body' => [
                    '_children' => [
                        [
                            'ParentTag' => [
                                '_attributes' => ['Target' => 'Test'],
                                '_children' => [
                                    ['Success' => ['_attributes' => ['Version' => '1.4']]],
                                    ['Success' => ['_attributes' => ['Version' => '1.5']]],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

try {
    $parentTag = Fluent::make($data)
        ->getRoot('SOAP-ENV:Envelope')
        ->getChild('SOAP-ENV:Body')
        ->getChild('ParentTag');

    $target = $parentTag->getAttribute('Target'); // "Test"

    $versions = $parentTag
        ->getChildren()
        ->filter(fn (Fluent $item, int|string $key, ?string $tag): bool => $tag === 'Success')
        ->pluck('Version'); // ['1.4', '1.5']
} catch (FluentException $e) {
    $error = $e->getMessage();
}
```

Useful `Fluent` methods:

- Navigation: `getRoot()`, `getChild()`, `getChildren()`, `getAttributes()`
- Value/metadata: `getValue()`, `toString()`, `hasChildren()`, `hasAttributes()`, `hasValue()`
- Collections: `each()`, `map()`, `filter()`, `first()`, `last()`, `at()`, `nth()`, `second()` ... `tenth()`
- Query helpers: `pluck()`, `contains()`, `count()`, `toArray()`, `isEmpty()`, `isNotEmpty()`

Changelog
---------

[](#changelog)

See [CHANGELOG](CHANGELOG.md).

Credits
-------

[](#credits)

- [Davide Maria Busi](https://github.com/davidemariabusi)
- [spatie/array-to-xml](https://github.com/spatie/array-to-xml)

License
-------

[](#license)

This project is released under the MIT License. See [LICENSE.md](LICENSE.md).

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance82

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 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

Every ~280 days

Total

5

Last Release

90d ago

PHP version history (3 changes)v0.1.0PHP &gt;=7.2

v0.1.1PHP &gt;=7.4

v0.1.4PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/49a3b093dfa95cd76a8b681f0aac2a7125d0c01403f8817de10ef56d209c3066?d=identicon)[davidemariabusi](/maintainers/davidemariabusi)

---

Top Contributors

[![davidemariabusi](https://avatars.githubusercontent.com/u/2246692?v=4)](https://github.com/davidemariabusi "davidemariabusi (10 commits)")

---

Tags

phpxmlarrayconverterdmb

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/dmb-xml-converter/health.svg)

```
[![Health](https://phpackages.com/badges/dmb-xml-converter/health.svg)](https://phpackages.com/packages/dmb-xml-converter)
```

PHPackages © 2026

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