Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66e31ea

Browse filesBrowse files
committed
[Serializer] Add XmlEncoder context example
1 parent ed90c27 commit 66e31ea
Copy full SHA for 66e31ea

File tree

Expand file treeCollapse file tree

1 file changed

+34
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+34
-0
lines changed

‎components/serializer.rst

Copy file name to clipboardExpand all lines: components/serializer.rst
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,40 @@ Option Description
991991
The ``decoder_ignored_node_types`` and ``encoder_ignored_node_types``
992992
options were introduced in Symfony 4.2.
993993

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+
9941028
The ``YamlEncoder``
9951029
~~~~~~~~~~~~~~~~~~~
9961030

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.