Skip to content

Commit cf34c42

Browse files
committed
minor #13269 addressed issue #11786 (armin-github)
This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead. Discussion ---------- addressed issue #11786 The following issue has been addressed: Repeated entry in the Serializer components doc #11786 The separate documentations for XMLEncoder were put into one section with minor changes in text. The documentation now contains one single section for XMLEncoder. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 0163ab3 addressed issue #11786
2 parents acfe6b6 + 0163ab3 commit cf34c42

File tree

1 file changed

+42
-68
lines changed

1 file changed

+42
-68
lines changed

components/serializer.rst

+42-68
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,20 @@ The ``XmlEncoder`` will encode this object like that::
841841
<bar>1</bar>
842842
</response>
843843

844-
Be aware that this encoder will consider keys beginning with ``@`` as attributes, and will use
845-
the key ``#comment`` for encoding XML comments::
844+
The special ``#`` key can be used to define the data of a node::
845+
846+
['foo' => ['@bar' => 'value', '#' => 'baz']];
847+
848+
// is encoded as follows:
849+
// <?xml version="1.0"?>
850+
// <response>
851+
// <foo bar="value">
852+
// baz
853+
// </foo>
854+
// </response>
855+
856+
Furthermore, keys beginning with ``@`` will be considered attributes, and
857+
the key ``#comment`` can be used for encoding XML comments::
846858

847859
$encoder = new XmlEncoder();
848860
$encoder->encode([
@@ -869,6 +881,34 @@ always as a collection.
869881
changed with the optional ``$encoderIgnoredNodeTypes`` argument of the
870882
``XmlEncoder`` class constructor.
871883

884+
The ``XmlEncoder`` Context Options
885+
..................................
886+
887+
The ``encode()`` method defines a third optional parameter called ``context``
888+
which defines the configuration options for the XmlEncoder an associative array::
889+
890+
$xmlEncoder->encode($array, 'xml', $context);
891+
892+
These are the options available:
893+
894+
``xml_format_output``
895+
If set to true, formats the generated XML with line breaks and indentation.
896+
897+
``xml_version``
898+
Sets the XML version attribute (default: ``1.1``).
899+
900+
``xml_encoding``
901+
Sets the XML encoding attribute (default: ``utf-8``).
902+
903+
``xml_standalone``
904+
Adds standalone attribute in the generated XML (default: ``true``).
905+
906+
``xml_root_node_name``
907+
Sets the root node name (default: ``response``).
908+
909+
``remove_empty_tags``
910+
If set to true, removes all empty tags in the generated XML (default: ``false``).
911+
872912
The ``YamlEncoder``
873913
~~~~~~~~~~~~~~~~~~~
874914

@@ -1192,72 +1232,6 @@ you indicate that you're expecting an array instead of a single object::
11921232
$data = ...; // The serialized data from the previous example
11931233
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
11941234

1195-
The ``XmlEncoder``
1196-
------------------
1197-
1198-
This encoder transforms arrays into XML and vice versa. For example, take an
1199-
object normalized as following::
1200-
1201-
['foo' => [1, 2], 'bar' => true];
1202-
1203-
The ``XmlEncoder`` encodes this object as follows:
1204-
1205-
.. code-block:: xml
1206-
1207-
<?xml version="1.0"?>
1208-
<response>
1209-
<foo>1</foo>
1210-
<foo>2</foo>
1211-
<bar>1</bar>
1212-
</response>
1213-
1214-
The array keys beginning with ``@`` are considered XML attributes::
1215-
1216-
['foo' => ['@bar' => 'value']];
1217-
1218-
// is encoded as follows:
1219-
// <?xml version="1.0"?>
1220-
// <response>
1221-
// <foo bar="value"/>
1222-
// </response>
1223-
1224-
Use the special ``#`` key to define the data of a node::
1225-
1226-
['foo' => ['@bar' => 'value', '#' => 'baz']];
1227-
1228-
// is encoded as follows:
1229-
// <?xml version="1.0"?>
1230-
// <response>
1231-
// <foo bar="value">baz</foo>
1232-
// </response>
1233-
1234-
Context
1235-
~~~~~~~
1236-
1237-
The ``encode()`` method defines a third optional parameter called ``context``
1238-
which defines the configuration options for the XmlEncoder an associative array::
1239-
1240-
$xmlEncoder->encode($array, 'xml', $context);
1241-
1242-
These are the options available:
1243-
1244-
``xml_format_output``
1245-
If set to true, formats the generated XML with line breaks and indentation.
1246-
1247-
``xml_version``
1248-
Sets the XML version attribute (default: ``1.1``).
1249-
1250-
``xml_encoding``
1251-
Sets the XML encoding attribute (default: ``utf-8``).
1252-
1253-
``xml_standalone``
1254-
Adds standalone attribute in the generated XML (default: ``true``).
1255-
1256-
``xml_root_node_name``
1257-
Sets the root node name (default: ``response``).
1258-
1259-
``remove_empty_tags``
1260-
If set to true, removes all empty tags in the generated XML (default: ``false``).
12611235

12621236
The ``CsvEncoder``
12631237
------------------

0 commit comments

Comments
 (0)