File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -991,6 +991,40 @@ Option Description
991
991
The ``decoder_ignored_node_types `` and ``encoder_ignored_node_types ``
992
992
options were introduced in Symfony 4.2.
993
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
+
994
1028
The ``YamlEncoder ``
995
1029
~~~~~~~~~~~~~~~~~~~
996
1030
You can’t perform that action at this time.
0 commit comments