Skip to content

[Serializer] XmlEncoder context #15435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -967,18 +967,20 @@ These are the options available:
============================== ================================================= ==========================
Option Description Default
============================== ================================================= ==========================
``xml_format_output`` If set to true, formats the generated XML with
line breaks and indentation.
``xml_format_output`` If set to true, formats the generated XML with ``false``
line breaks and indentation
``xml_version`` Sets the XML version attribute ``1.1``
``xml_encoding`` Sets the XML encoding attribute ``utf-8``
``xml_standalone`` Adds standalone attribute in the generated XML ``true``
``xml_type_cast_attributes`` This provides the ability to forgot the attribute ``true``
type casting
``xml_root_node_name`` Sets the root node name (default: ``response``).
``as_collection`` Always returns results as a collection, even if
``xml_root_node_name`` Sets the root node name ``response``
``as_collection`` Always returns results as a collection, even if ``false``
only one line is decoded
``decoder_ignored_node_types`` Sets nodes to be ignored in the decode ``[\XML_PI_NODE, \XML_COMMENT_NODE]``
``encoder_ignored_node_types`` Sets nodes to be ignored in the encode ``[]``
``decoder_ignored_node_types`` Array of node types (`DOM XML_* constants`_) ``[\XML_PI_NODE, \XML_COMMENT_NODE]``
to be ignored while decoding
``encoder_ignored_node_types`` Array of node types (`DOM XML_* constants`_) ``[]``
to be ignored while encoding
``load_options`` XML loading `options with libxml`_ ``\LIBXML_NONET | \LIBXML_NOBLANKS``
``remove_empty_tags`` If set to true, removes all empty tags in the ``false``
generated XML
Expand All @@ -989,6 +991,40 @@ Option Description
The ``decoder_ignored_node_types`` and ``encoder_ignored_node_types``
options were introduced in Symfony 4.2.

Example with custom ``context``::

use Symfony\Component\Serializer\Encoder\XmlEncoder;

// create encoder with specified options as new default settings
$xmlEncoder = new XmlEncoder(['xml_format_output' => true]);

$data = [
'id' => 'IDHNQIItNyQ',
'date' => '2019-10-24',
];

// encode with default context
$xmlEncoder->encode($data, 'xml');
// outputs:
// <?xml version="1.0"?>
// <response>
// <id>IDHNQIItNyQ</id>
// <date>2019-10-24</date>
// </response>

// encode with modified context
$xmlEncoder->encode($data, 'xml', [
'xml_root_node_name' => 'track',
'encoder_ignored_node_types' => [
\XML_PI_NODE, // removes XML declaration (the leading xml tag)
],
]);
// outputs:
// <track>
// <id>IDHNQIItNyQ</id>
// <date>2019-10-24</date>
// </track>

The ``YamlEncoder``
~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1555,6 +1591,7 @@ Learn more
.. _`JMS serializer`: https://github.com/schmittjoh/serializer
.. _RFC3339: https://tools.ietf.org/html/rfc3339#section-5.8
.. _`options with libxml`: https://www.php.net/manual/en/libxml.constants.php
.. _`DOM XML_* constants`: https://www.php.net/manual/en/dom.constants.php
.. _JSON: http://www.json.org/
.. _XML: https://www.w3.org/XML/
.. _YAML: https://yaml.org/
Expand Down