@@ -967,18 +967,20 @@ These are the options available:
967
967
============================== ================================================= ==========================
968
968
Option Description Default
969
969
============================== ================================================= ==========================
970
- ``xml_format_output `` If set to true, formats the generated XML with
971
- line breaks and indentation.
970
+ ``xml_format_output `` If set to true, formats the generated XML with `` false ``
971
+ line breaks and indentation
972
972
``xml_version `` Sets the XML version attribute ``1.1 ``
973
973
``xml_encoding `` Sets the XML encoding attribute ``utf-8 ``
974
974
``xml_standalone `` Adds standalone attribute in the generated XML ``true ``
975
975
``xml_type_cast_attributes `` This provides the ability to forgot the attribute ``true ``
976
976
type casting
977
- ``xml_root_node_name `` Sets the root node name (default: ``response ``).
978
- ``as_collection `` Always returns results as a collection, even if
977
+ ``xml_root_node_name `` Sets the root node name ``response ``
978
+ ``as_collection `` Always returns results as a collection, even if `` false ``
979
979
only one line is decoded
980
- ``decoder_ignored_node_types `` Sets nodes to be ignored in the decode ``[\XML_PI_NODE, \XML_COMMENT_NODE] ``
981
- ``encoder_ignored_node_types `` Sets nodes to be ignored in the encode ``[] ``
980
+ ``decoder_ignored_node_types `` Array of node types (`DOM XML_* constants `_) ``[\XML_PI_NODE, \XML_COMMENT_NODE] ``
981
+ to be ignored while decoding
982
+ ``encoder_ignored_node_types `` Array of node types (`DOM XML_* constants `_) ``[] ``
983
+ to be ignored while encoding
982
984
``load_options `` XML loading `options with libxml `_ ``\LIBXML_NONET | \LIBXML_NOBLANKS ``
983
985
``remove_empty_tags `` If set to true, removes all empty tags in the ``false ``
984
986
generated XML
@@ -989,6 +991,40 @@ Option Description
989
991
The ``decoder_ignored_node_types `` and ``encoder_ignored_node_types ``
990
992
options were introduced in Symfony 4.2.
991
993
994
+ Example with custom ``context ``::
995
+
996
+ use Symfony\Component\Serializer\Encoder\XmlEncoder;
997
+
998
+ // create encoder with specified options as new default settings
999
+ $xmlEncoder = new XmlEncoder(['xml_format_output' => true]);
1000
+
1001
+ $data = [
1002
+ 'id' => 'IDHNQIItNyQ',
1003
+ 'date' => '2019-10-24',
1004
+ ];
1005
+
1006
+ // encode with default context
1007
+ $xmlEncoder->encode($data, 'xml');
1008
+ // outputs:
1009
+ // <?xml version="1.0"?>
1010
+ // <response>
1011
+ // <id>IDHNQIItNyQ</id>
1012
+ // <date>2019-10-24</date>
1013
+ // </response>
1014
+
1015
+ // encode with modified context
1016
+ $xmlEncoder->encode($data, 'xml', [
1017
+ 'xml_root_node_name' => 'track',
1018
+ 'encoder_ignored_node_types' => [
1019
+ \XML_PI_NODE, // removes XML declaration (the leading xml tag)
1020
+ ],
1021
+ ]);
1022
+ // outputs:
1023
+ // <track>
1024
+ // <id>IDHNQIItNyQ</id>
1025
+ // <date>2019-10-24</date>
1026
+ // </track>
1027
+
992
1028
The ``YamlEncoder ``
993
1029
~~~~~~~~~~~~~~~~~~~
994
1030
@@ -1555,6 +1591,7 @@ Learn more
1555
1591
.. _`JMS serializer` : https://github.com/schmittjoh/serializer
1556
1592
.. _RFC3339 : https://tools.ietf.org/html/rfc3339#section-5.8
1557
1593
.. _`options with libxml` : https://www.php.net/manual/en/libxml.constants.php
1594
+ .. _`DOM XML_* constants` : https://www.php.net/manual/en/dom.constants.php
1558
1595
.. _JSON : http://www.json.org/
1559
1596
.. _XML : https://www.w3.org/XML/
1560
1597
.. _YAML : https://yaml.org/
0 commit comments