PHPackages                             michelmelo/response-xml - 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. michelmelo/response-xml

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

michelmelo/response-xml
=======================

The missing XML support for Laravel's Response class.

1.1.3(6y ago)0253PHPPHP ~7.0

Since Oct 22Pushed 6y agoCompare

[ Source](https://github.com/michelmelo/response-xml)[ Packagist](https://packagist.org/packages/michelmelo/response-xml)[ RSS](/packages/michelmelo-response-xml/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (3)Dependencies (7)Versions (8)Used By (0)

The missing XML support for Laravel's Response class.

This package is designed to work with the [Laravel](https://laravel.com) framework.

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

[](#installation)

Install via composer:

```
composer require michelmelo/response-xml

```

### Registering the service provider

[](#registering-the-service-provider)

For Laravel 5.4 and lower, add the following line to your `config/app.php`:

```
/*
 * Package Service Providers...
 */
MichelMelo\ResponseXml\Providers\ResponseXmlServiceProvider::class,
```

For Laravel 5.5 and greater, the package will auto register the provider for you.

### Using Lumen

[](#using-lumen)

To register the service provider, add the following line to `app/bootstrap/app.php`:

```
$app->register(MichelMelo\ResponseXml\Providers\ResponseXmlServiceProvider::class);
```

Quick start
-----------

[](#quick-start)

### Respond with xml

[](#respond-with-xml)

```
$data = [
    'status' => 'success',
    'data' => [
        'first_name' => 'John',
        'last_name' => 'Smith',
    ]
];
return response()->xml($data);
```

Returns:

```

    success

        John
        Smith

```

### Respond with xml from a collection or eloquent query

[](#respond-with-xml-from-a-collection-or-eloquent-query)

You may also pass a collection to be transformed into xml.

```
return response()->xml(User::all());
```

### Respond with existing xml

[](#respond-with-existing-xml)

If you already have xml, you can pass it to the `xml` method to respond.

```
$xml =  'success',
    'data' => [
        'first_name' => 'John',
        'last_name' => 'Smith',
    ]
];
return response()->preferredFormat($data);
```

### Adding attributes

[](#adding-attributes)

You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.

```
$array = [
    'Good guy' => [
        '_attributes' => ['attr1' => 'value'],
        'name' => 'Luke Skywalker',
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ],
    'The survivor' => [
        '_attributes' => ['house'=>'Hogwarts'],
        '_value' => 'Harry Potter'
    ]
];

return response()->preferredForma($array);
```

This code will result in:

```

        Luke Skywalker
        Lightsaber

        Sauron
        Evil Eye

        Harry Potter

```

### Using reserved characters

[](#using-reserved-characters)

It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.

```
$array = [
    'Good guy' => [
        'name' => [
            '_cdata' => 'Luke Skywalker'
        ],
        'weapon' => 'Lightsaber'
    ],
    'Bad guy' => [
        'name' => 'Sauron',
        'weapon' => 'Evil Eye'
    ]
];

return response()->preferredForma($array);
```

This code will result in:

```

        Luke Skywalker]]>
        Lightsaber

        &lt;h1&gt;Sauron&lt;/h1&gt;
        Evil Eye

```

Methods and arguments
---------------------

[](#methods-and-arguments)

**Response method**

`->xml($xml, $status = 200, array $headers = [], $xmlRoot = 'response')`

The `$xml` argument is the data you want to be transformed into xml (may also be a premade xml string).

`$status` is the http code your response will send.

`$headers` is an array of key/values of http headers your response will return.

`$xmlRoot` will change the root xml element. Default is `response`.

**Response method**

`->preferredFormat($data, $status = 200, array $headers = [], $xmlRoot = 'response')`

See `->xml()` method arguments.

The only difference between this method and `->xml()` is `$data` can potentially be transformed to json and `$xmlRoot` will be ignored if the response is json.

Purpose
-------

[](#purpose)

Have you ever found yourself wishing Laravel offered the same exemplary support for returning XML responses as it does for JSON? Imagine you are creating an api platform and want to be inclusive of other apps that would prefer to make XML requests to your application - sometimes the reasons are more than preferential. Wouldn't it be a dream if you could return XML as simply as writing `return response()->xml($data);` and it just worked? **Now you can!**

This package achieves one critical goal: respond with XML as easily as you can with JSON in your Laravel application.

\##more

Credits
-------

[](#credits)

- [Mark Townsend ](https://github.com/mtownsend5512)
- [Spatie](https://github.com/spatie/array-to-xml)
- All Contributors

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 62.5% 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 ~68 days

Recently: every ~19 days

Total

7

Last Release

2356d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d756073dec37ac9abd3c7edd0cf00b94f2d2de0055531eba5472392b1fd4da2?d=identicon)[michelmelo](/maintainers/michelmelo)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (10 commits)")[![michelmelo](https://avatars.githubusercontent.com/u/159223?v=4)](https://github.com/michelmelo "michelmelo (6 commits)")

---

Tags

responselaravelxml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/michelmelo-response-xml/health.svg)

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

71510.9M66](/packages/laravel-mcp)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[mtownsend/response-xml

The missing XML support for Laravel's Response class.

1041.2M3](/packages/mtownsend-response-xml)[bmatovu/laravel-xml

Laravel XML Support

91270.4k](/packages/bmatovu-laravel-xml)

PHPackages © 2026

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