PHPackages                             bmatovu/laravel-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. bmatovu/laravel-xml

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

bmatovu/laravel-xml
===================

Laravel XML Support

v4.2.0(2mo ago)91288.2k↓52%10[5 issues](https://github.com/mtvbrianking/laravel-xml/issues)[3 PRs](https://github.com/mtvbrianking/laravel-xml/pulls)MITPHPPHP ^8.2CI failing

Since Jul 2Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/mtvbrianking/laravel-xml)[ Packagist](https://packagist.org/packages/bmatovu/laravel-xml)[ Docs](https://github.com/mtvbrianking/laravel-xml)[ RSS](/packages/bmatovu-laravel-xml/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (14)Versions (22)Used By (0)

Laravel XML Support Package
---------------------------

[](#laravel-xml-support-package)

[![](./art/banner.png)](./art/banner.png)

[![Total Downloads](https://camo.githubusercontent.com/e0ebc319f9b091915c1d0b609bccb72a815ef4a5e21aefadf5d84be90331b52a/68747470733a2f2f706f7365722e707567782e6f72672f626d61746f76752f6c61726176656c2d786d6c2f646f776e6c6f616473)](https://packagist.org/packages/bmatovu/laravel-xml)[![Latest Stable Version](https://camo.githubusercontent.com/1e8fbe7c06e23eaf62637220514f4dffd780044ebb7a004563013a37bee094ae/68747470733a2f2f706f7365722e707567782e6f72672f626d61746f76752f6c61726176656c2d786d6c2f762f737461626c65)](https://packagist.org/packages/bmatovu/laravel-xml)[![License](https://camo.githubusercontent.com/f6035a6b1498daf6ccbb4697b7e18b17316a7cc3d6eb7fd207808c99f5803fa4/68747470733a2f2f706f7365722e707567782e6f72672f626d61746f76752f6c61726176656c2d786d6c2f6c6963656e7365)](https://packagist.org/packages/bmatovu/laravel-xml)[![Quality](https://camo.githubusercontent.com/3dd3cdad8b791b9d7ebab1d4f9af365c383565bd0c038e4b8910cbcc8903295e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7476627269616e6b696e672f6c61726176656c2d786d6c2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mtvbrianking/laravel-xml/?branch=master)[![Coverage](https://camo.githubusercontent.com/52d525314d0befc09004abd388b8bdae0a9b5078aa74b00483c02892f5f8f6c4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d7476627269616e6b696e672f6c61726176656c2d786d6c2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mtvbrianking/laravel-xml/?branch=master)[![Unit Tests](https://github.com/mtvbrianking/laravel-xml/workflows/run-tests/badge.svg)](https://github.com/mtvbrianking/laravel-xml/actions?query=workflow:run-tests)[![Documentation](https://github.com/mtvbrianking/laravel-xml/workflows/gen-docs/badge.svg)](https://mtvbrianking.github.io/laravel-xml/master)

This package comes with the much desired xml support for you Laravel project.

### Installation

[](#installation)

```
composer require bmatovu/laravel-xml
```

### Requests

[](#requests)

Get the request content (body).

```
$request->xml();
```

\* Returns `Bmatovu\LaravelXml\Support\XMLElement` object.

Determine if the request content type is XML.

```
$request->sentXml();
```

Determine if the current request is accepting XML.

```
$request->wantsXml();
```

Validate XML content

```
$isValid = Xml::is_valid($request->xml());

if (! $isValid) {
    return response()->xml(['message' => 'The given data was malformed.'], 400);
}
```

**Validation** - Against XML Schema Definition

```
$errors = Xml::validate($request->xml(), 'path_to/sample.xsd');

if ($errors) {
    return response()->xml([
        'message' => 'The given data was invalid.',
        'errors' => $errors,
    ], 422);
}
```

### Responses

[](#responses)

```
Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml($user);
});
```

```

    1
    jdoe
    jdoe@example.com

```

```
Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml(['user' => $user->toArray()]);
});
```

```

        1
        jdoe
        jdoe@example.com

```

```
Route::get('/users/{user}', function (Request $request, int $userId) {
    $user = User::findOrFail($userId);

    return response()->xml($user, 200, [], ['root' => 'user']);
});
```

```

    1
    jdoe
    jdoe@example.com

```

```
Route::get('/users', function () {
    $users = User::get();

    return response()->xml(['users' => $users->toArray()]);
});
```

```

        1
        John Doe
        jdoe@example.com

        2
        Gary Plant
        gplant@example.com

```

And will automatically set the content type to xml

`Content-Type → text/xml; charset=UTF-8`

### Middleware

[](#middleware)

First register the middleware in `app\Http\Kernel.php`

```
protected $routeMiddleware = [
    // ...
    'xml' => \Bmatovu\LaravelXml\Http\Middleware\RequireXml::class,
];
```

Then use the middleware on your routes, or in the controllers.

```
Route::post('/users', function (Request, $request) {
    // do something...
})->middleware('xml');
```

This middleware only checks the `Content-Type` by defaul;

\[`415` - **Unsupported Media Type**\]

```

    Only accepting content of type XML.

```

To check is the passed content is valid XML, pass a bool to the middleware

```
Route::post('/users', function (Request, $request) {
    // do something...
})->middleware('xml:1');
```

\[`400` - **Bad Request**\]

```

    The given data was malformed.

```

### Utilities

[](#utilities)

**Encode: Array to Xml**

```
Xml::encode(['key' => 'value']);
```

Or

```
xml_encode(['key' => 'value']);
```

**Decode: Xml to Array**

```
Xml::decode('value');
```

Or

```
xml_decode('value');
```

---

Credits
-------

[](#credits)

Under the hood, I'm using;

[Spatie's array to XML convernsion](https://github.com/spatie/array-to-xml)

[Hakre's XML to JSON conversion](https://hakre.wordpress.com/2013/07/09/simplexml-and-json-encode-in-php-part-i)

[Akande's XML validation](https://medium.com/@Sirolad/validating-xml-against-xsd-in-php-5607f725955a)

Reporting bugs
--------------

[](#reporting-bugs)

If you've stumbled across a bug, please help us by leaving as much information about the bug as possible, e.g.

- Steps to reproduce
- Expected result
- Actual result

This will help us to fix the bug as quickly as possible, and if you do wish to fix it yourself; feel free to [fork the package on GitHub](https://github.com/mtvbrianking/laravel-xml) and submit a pull request!

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity49

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 96.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 ~149 days

Recently: every ~290 days

Total

20

Last Release

80d ago

Major Versions

1.x-dev → v2.0.02020-03-10

v2.0.0 → v3.0.02020-09-13

v3.2.1 → v4.0.02024-04-16

PHP version history (5 changes)1.0.0PHP &gt;=5.6.4

v2.0.0PHP ^7.2.5

v3.1.0PHP ^8.0

v4.0.0PHP ^8.1

v4.2.0PHP ^8.2

### Community

Maintainers

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

---

Top Contributors

[![mtvbrianking](https://avatars.githubusercontent.com/u/5412360?v=4)](https://github.com/mtvbrianking "mtvbrianking (126 commits)")[![Poket-Jony](https://avatars.githubusercontent.com/u/15607518?v=4)](https://github.com/Poket-Jony "Poket-Jony (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")[![trollfalgar](https://avatars.githubusercontent.com/u/441455?v=4)](https://github.com/trollfalgar "trollfalgar (1 commits)")

---

Tags

laravelmiddlewarepackageresponsesupportxmlresponserequestmiddlewarelaravelxmlpackage

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/bmatovu-laravel-xml/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.5k55.4M8.5k](/packages/larastan-larastan)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45444.2k1](/packages/pressbooks-pressbooks)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5022.0k](/packages/simplestats-io-laravel-client)[calebdw/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

15118.7k4](/packages/calebdw-larastan)

PHPackages © 2026

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