Skip to content

Commit ad38fcc

Browse files
authored
Adding precision on how to define attributes on the root node for the XML Encoder
1 parent 4e3a968 commit ad38fcc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

components/serializer.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,29 @@ the key ``#comment`` can be used for encoding XML comments::
10971097
You can pass the context key ``as_collection`` in order to have the results
10981098
always as a collection.
10991099

1100+
.. note::
1101+
1102+
You may need to add some attributes on the root node. This can be done this way :
1103+
1104+
.. code-block:: php
1105+
<?php
1106+
$encoder = new XmlEncoder();
1107+
$encoder->encode([
1108+
'@attribute1' => 'foo',
1109+
'@attribute2' => 'bar',
1110+
'#' => ['foo' => ['@bar' => 'value', '#' => 'baz']]
1111+
], 'xml');
1112+
1113+
This will generate the following XML :
1114+
1115+
.. code-block:: xml
1116+
<?xml version="1.0"?>
1117+
<response attribute1="foo" attribute2="bar">
1118+
<foo bar="value">
1119+
baz
1120+
</foo>
1121+
</response>
1122+
11001123
.. tip::
11011124

11021125
XML comments are ignored by default when decoding contents, but this

0 commit comments

Comments
 (0)