8000 [Serializer] Add XmlEncoder context example · symfony/symfony-docs@66e31ea · GitHub
[go: up one dir, main page]

Skip to content

Commit 66e31ea

Browse files
committed
[Serializer] Add XmlEncoder context example
1 parent ed90c27 commit 66e31ea

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

components/serializer.rst

Lines 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