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

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

michalcarson/symfony-xml-response
=================================

A simple XML Response formatter for Symfony.

1.1.0(10y ago)17794[1 PRs](https://github.com/michalcarson/SymfonyXmlResponse/pulls)MITPHPPHP &gt;=5.3.0

Since Dec 13Pushed 9y ago2 watchersCompare

[ Source](https://github.com/michalcarson/SymfonyXmlResponse)[ Packagist](https://packagist.org/packages/michalcarson/symfony-xml-response)[ Docs](http://www.carsonsoftwareengineering.com)[ RSS](/packages/michalcarson-symfony-xml-response/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

SymfonyXmlResponse
==================

[](#symfonyxmlresponse)

A simple XML Response formatter for Symfony.

Is it pointless?
----------------

[](#is-it-pointless)

Someday, someone will probably tell me this whole class was unnecessary and if I just knew more about Symfony, I could have done this a much easier way. That probably won't happen until I've poured a thousand hours into this project. But, still, tell me!

The point
---------

[](#the-point)

This class is an effort to make it possible to return an array from an action and have it rendered into XML. This should happen just the same way you can return an array and have it rendered into JSON. The action (controller...whatever term you like) shouldn't have to know the format of the response, just as it shouldn't know the format of the request.

I'm using this with [AOL/ATC](https://github.com/aol/atc). ATC is itself based on [Aura.Router](https://github.com/auraphp/Aura.Router). Within ATC, I have a custom presenter that--once it determines the caller wants an XML response--simply does this:

```
$xr = new XmlResponse();

// changing the default root element
$xr->root_element_name = $view;

// data must be set after the root element
$response = $xr->setData($data);

return $response;
```

In the code above, `$data` is an array and `$view` is a string.

If the default root element name ("document") is acceptable, this can be further simplified to:

```
$response = new XmlResponse($data);
return $response;
```

or

```
return new XmlResponse($data);
```

or

```
// using the static create() method
$response = XmlResponse::create($data);
return $response;
```

The static `create()` method and the constructor both accept additional optional parameters to specify the status code and an array of headers. (The default status code is 200.)

```
// specifying optional return code and headers
$response = new XmlResponse($data, 202, array('Foo-Header' => 'bar'));
return $response;
```

Data array
----------

[](#data-array)

The data passed to this class should be an associative array. Each array key represents an XML tag name and the array value represents the content of the XML tag.

```
$data = array(
    'foo' => 'bar',
    'argle' => 'bargle'
);
$response = new XmlResponse($data);
return $response;
```

The array above will produce this XML output:

```

  bar
  bargle

```

Attributes
----------

[](#attributes)

You can specify attributes to a tag by passing a specially constructed array for the value of that tag. Within this array, each attribute should be represented as a key value pair with the key prefixed by '@'. The actual value of the tag should have a key which repeats the original key name. All attribute key-value pairs must come before the actual value entry.

Keep in mind, this is crossing some boundaries for an action to supply XML attributes. If this array were rendered as JSON instead of XML, it would probably not be usable. This design is flawed and will need to be changed in the future.

```
$data = array(
    'foo' => array(
        '@buzz' => 'boom',
        '@bing' => 'bam',
        'foo' => 'bar'       // same key name as parent
    ),
    'argle' => 'bargle'
);
$response = new XmlResponse($data);
return $response;
```

The array above will produce this XML output:

```

  bar
  bargle

```

Repeating Elements
------------------

[](#repeating-elements)

XML can have repeating elements.

```

  ipsum

    file1.txt10K
    file2.txt20K
    file3.txt8K

  dolore

```

PHP arrays (and JavaScript objects) do not support this without some very contrived structures. In order to support repeating fields, use the XmlRepeater class. XmlRepeater acts as a decorator to the XmlResponse class.

You may add as many XmlRepeater instances as you need. Each of them takes a placeholder, an element name and an array of data. The class will turn the array into XML, wrap it in an element with the given name and insert it into the XmlResponse result in place of the placeholder.

```
// associative array for XmlResponse
$data = [
    'lorem' => 'ipsum',
    'files' => '@filesPlaceHolder@'
    'et' => 'dolore'
];

// indexed array for the repeating data
$file_array = [
    ['name' => 'file1.txt', 'size' => '10K'],
    ['name' => 'file2.txt', 'size' => '20K'],
    ['name' => 'file3.txt', 'size' => '8K']
];

$repeater = new XmlRepeater('@filesPlaceHolder@', 'file', $file_array);

$response = new XmlResponse($data);
$response->addDecorator($repeater);
return $response;
```

This code would produce the XML document above with repeating "file" elements inside the "files" element.

License
-------

[](#license)

Licensed under the MIT license.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~1 days

Total

2

Last Release

3809d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5287400?v=4)[Michal Carson](/maintainers/michalcarson)[@michalcarson](https://github.com/michalcarson)

---

Top Contributors

[![michalcarson](https://avatars.githubusercontent.com/u/5287400?v=4)](https://github.com/michalcarson "michalcarson (9 commits)")

---

Tags

responsesymfonyxml

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[presta/sitemap-bundle

A Symfony bundle that provides tools to build your application sitemap.

3929.4M28](/packages/presta-sitemap-bundle)[rumenx/php-sitemap

Framework-agnostic Sitemap generator for PHP, Laravel, and Symfony.

1.3k15.1k1](/packages/rumenx-php-sitemap)[bmatovu/laravel-xml

Laravel XML Support

91270.4k](/packages/bmatovu-laravel-xml)[eprofos/user-agent-analyzer

A powerful Symfony bundle for user-agent analysis. It provides accurate detection of operating systems (Windows, MacOS, Linux, iOS, Android...), browsers (Chrome, Firefox, Safari...), and device types (Desktop, Mobile, Tablet, TV...). Supports specific version detection and includes advanced handling of special cases like WebViews and compatibility modes. Features comprehensive logging and detailed analysis results.

182.3k](/packages/eprofos-user-agent-analyzer)

PHPackages © 2026

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