8000 [Serializer] XmlEncoder context · symfony/symfony-docs@177e66f · GitHub
[go: up one dir, main page]

Skip to content

Commit 177e66f

Browse files
SirRFIjaviereguiluz
authored andcommitted
[Serializer] XmlEncoder context
1 parent e556d26 commit 177e66f

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

components/serializer.rst

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -967,18 +967,20 @@ These are the options available:
967967
============================== ================================================= ==========================
968968
Option Description Default
969969
============================== ================================================= ==========================
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
972972
``xml_version`` Sets the XML version attribute ``1.1``
973973
``xml_encoding`` Sets the XML encoding attribute ``utf-8``
974974
``xml_standalone`` Adds standalone attribute in the generated XML ``true``
975975
``xml_type_cast_attributes`` This provides the ability to forgot the attribute ``true``
976976
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``
979979
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
982984
``load_options`` XML loading `options with libxml`_ ``\LIBXML_NONET | \LIBXML_NOBLANKS``
983985
``remove_empty_tags`` If set to true, removes all empty tags in the ``false``
984986
generated XML
@@ -989,6 +991,40 @@ Option Description
989991
The ``decoder_ignored_node_types`` and ``encoder_ignored_node_types``
990992
options were introduced in Symfony 4.2.
991993

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+
9921028
The ``YamlEncoder``
9931029
~~~~~~~~~~~~~~~~~~~
9941030

@@ -1555,6 +1591,7 @@ Learn more
15551591
.. _`JMS serializer`: https://github.com/schmittjoh/serializer
15561592
.. _RFC3339: https://tools.ietf.org/html/rfc3339#section-5.8
15571593
.. _`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
15581595
.. _JSON: http://www.json.org/
15591596
.. _XML: https://www.w3.org/XML/
15601597
.. _YAML: https://yaml.org/

0 commit comments

Comments
 (0)
0