# XmlResponseFormatter

> XmlResponseFormatter formats the given data into an XML response content.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › XmlResponseFormatter

| class | `yii\web\XmlResponseFormatter` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`ResponseFormatterInterface`](/docs/api/yii/web/responseformatterinterface/index.md), [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/XmlResponseFormatter.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/XmlResponseFormatter.php#L26) |

XmlResponseFormatter formats the given data into an XML response content.

It is used by [`Response`](/docs/api/yii/web/response/index.md) to format response data.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-contentType"></span>`$contentType` | `string` | The Content-Type header for the response Default: `'application/xml'`. | `XmlResponseFormatter` |
| [`$encoding`](#property-encoding) | `string\|null` | The XML encoding. Default: `null`. | `XmlResponseFormatter` |
| <span id="property-itemTag"></span>`$itemTag` | `string` | The name of the elements that represent the array elements with numeric keys. Default: `'item'`. | `XmlResponseFormatter` |
| <span id="property-objectTagToLowercase"></span>`$objectTagToLowercase` | `bool` | If true, converts object tags to lowercase, `$useObjectTags` must be enabled Default: `false`. Available since 2.0.43. | `XmlResponseFormatter` |
| [`$rootTag`](#property-rootTag) | `string\|string[]\|null\|false` | The name of the root element. Default: `'response'`. | `XmlResponseFormatter` |
| <span id="property-useObjectTags"></span>`$useObjectTags` | `bool` | If object class names should be used as tag names Default: `true`. Available since 2.0.11. | `XmlResponseFormatter` |
| [`$useTraversableAsArray`](#property-useTraversableAsArray) | `bool` | Whether to interpret objects implementing the [`Traversable`](https://www.php.net/manual/en/class.traversable.php) interface as arrays. Default: `true`. Available since 2.0.7. | `XmlResponseFormatter` |
| <span id="property-version"></span>`$version` | `string` | The XML version Default: `'1.0'`. | `XmlResponseFormatter` |
| <span id="property-dom"></span>`$dom` protected | [`DOMDocument`](https://www.php.net/manual/en/class.domdocument.php) | The XML document, serves as the root of the document tree Default: `null`. Available since 2.0.43. | `XmlResponseFormatter` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`format()`](#method-format) | Formats the specified response. | `XmlResponseFormatter` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`buildXml()`](#method-buildXml) protected | Recursively adds data to XML document. | `XmlResponseFormatter` |
| [`formatScalarValue()`](#method-formatScalarValue) protected | Formats scalar value to use in XML text node. | `XmlResponseFormatter` |
| [`getValidXmlElementName()`](#method-getValidXmlElementName) protected | Returns element name ready to be used in DOMElement if name is not empty, is not int and is valid. | `XmlResponseFormatter` |
| [`isValidXmlName()`](#method-isValidXmlName) protected | Checks if name is valid to be used in XML. | `XmlResponseFormatter` |

## Property details

### $encoding <span id="property-encoding"></span>

```php
public string|null $encoding = null
```

The XML encoding.

If not set, it will use the value of [`Response::$charset`](/docs/api/yii/web/response/index.md#property-charset).

### $rootTag <span id="property-rootTag"></span>

```php
public string|string[]|null|false $rootTag = 'response'
```

The name of the root element.

If set to false, null or is empty then no root tag
should be added.

Since 2.0.44 URI namespace could be specified by passing `[namespace, tag name]` array.

### $useTraversableAsArray <span id="property-useTraversableAsArray"></span>

```php
public bool $useTraversableAsArray = true
```

Whether to interpret objects implementing the [`Traversable`](https://www.php.net/manual/en/class.traversable.php) interface as arrays.

Defaults to `true`.

**Since**

2.0.7

## Method details

### format() <span id="method-format"></span>

```php
public function format(yii\web\Response $response): mixed
```

Formats the specified response.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | [`Response`](/docs/api/yii/web/response/index.md) | The response to be formatted. |

**Returns**

`mixed`

### buildXml() <span id="method-buildXml"></span>

```php
protected function buildXml(DOMElement|DOMDocument $element, mixed $data): mixed
```

Recursively adds data to XML document.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$element` | [`DOMElement`](https://www.php.net/manual/en/class.domelement.php)`\|`[`DOMDocument`](https://www.php.net/manual/en/class.domdocument.php) | Current element |
| `$data` | `mixed` | Content of the current element |

**Returns**

`mixed`

### formatScalarValue() <span id="method-formatScalarValue"></span>

```php
protected function formatScalarValue(int|string|bool|float $value): string
```

Formats scalar value to use in XML text node.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `int\|string\|bool\|float` | A scalar value. |

**Returns**

`string`: String representation of the value.

**Since**

2.0.11

### getValidXmlElementName() <span id="method-getValidXmlElementName"></span>

```php
protected function getValidXmlElementName(mixed $name): string
```

Returns element name ready to be used in DOMElement if
name is not empty, is not int and is valid.

Falls back to [`$itemTag`](/docs/api/yii/web/xmlresponseformatter/index.md#property-itemTag) otherwise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The original name |

**Returns**

`string`

**Since**

2.0.12

### isValidXmlName() <span id="method-isValidXmlName"></span>

```php
protected function isValidXmlName(mixed $name): bool
```

Checks if name is valid to be used in XML.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The name to test |

**Returns**

`bool`

**Since**

2.0.12

**See also**

- <https://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943>