@@ -1080,23 +1080,59 @@ These are the options available:
1080
1080
============================== ================================================= ==========================
1081
1081
Option Description Default
1082
1082
============================== ================================================= ==========================
1083
- ``xml_format_output `` If set to true, formats the generated XML with
1084
- line breaks and indentation.
1083
+ ``xml_format_output `` If set to true, formats the generated XML with `` false ``
1084
+ line breaks and indentation
1085
1085
``xml_version `` Sets the XML version attribute ``1.1 ``
1086
1086
``xml_encoding `` Sets the XML encoding attribute ``utf-8 ``
1087
1087
``xml_standalone `` Adds standalone attribute in the generated XML ``true ``
1088
1088
``xml_type_cast_attributes `` This provides the ability to forgot the attribute ``true ``
1089
1089
type casting
1090
- ``xml_root_node_name `` Sets the root node name (default: ``response ``).
1091
- ``as_collection `` Always returns results as a collection, even if
1090
+ ``xml_root_node_name `` Sets the root node name ``response ``
1091
+ ``as_collection `` Always returns results as a collection, even if `` false ``
1092
1092
only one line is decoded
1093
- ``decoder_ignored_node_types `` Sets nodes to be ignored in the decode ``[\XML_PI_NODE, \XML_COMMENT_NODE] ``
1094
- ``encoder_ignored_node_types `` Sets nodes to be ignored in the encode ``[] ``
1093
+ ``decoder_ignored_node_types `` Array of node types (`DOM XML_* constants `_) ``[\XML_PI_NODE, \XML_COMMENT_NODE] ``
1094
+ to be ignored while decoding
1095
+ ``encoder_ignored_node_types `` Array of node types (`DOM XML_* constants `_) ``[] ``
1096
+ to be ignored while encoding
1095
1097
``load_options `` XML loading `options with libxml `_ ``\LIBXML_NONET | \LIBXML_NOBLANKS ``
1096
1098
``remove_empty_tags `` If set to true, removes all empty tags in the ``false ``
1097
1099
generated XML
1098
1100
============================== ================================================= ==========================
1099
1101
1102
+ Example with custom ``context ``::
1103
+
1104
+ use Symfony\Component\Serializer\Encoder\XmlEncoder;
1105
+
1106
+ // create encoder with specified options as new default settings
1107
+ $xmlEncoder = new XmlEncoder(['xml_format_output' => true]);
1108
+
1109
+ $data = [
1110
+ 'id' => 'IDHNQIItNyQ',
1111
+ 'date' => '2019-10-24',
1112
+ ];
1113
+
1114
+ // encode with default context
1115
+ $xmlEncoder->encode($data, 'xml');
1116
+ // outputs:
1117
+ // <?xml version="1.0"?>
1118
+ // <response>
1119
+ // <id>IDHNQIItNyQ</id>
1120
+ // <date>2019-10-24</date>
1121
+ // </response>
1122
+
1123
+ // encode with modified context
1124
+ $xmlEncoder->encode($data, 'xml', [
1125
+ 'xml_root_node_name' => 'track',
1126
+ 'encoder_ignored_node_types' => [
1127
+ \XML_PI_NODE, // removes XML declaration (the leading xml tag)
1128
+ ],
1129
+ ]);
1130
+ // outputs:
1131
+ // <track>
1132
+ // <id>IDHNQIItNyQ</id>
1133
+ // <date>2019-10-24</date>
1134
+ // </track>
1135
+
1100
1136
The ``YamlEncoder ``
1101
1137
~~~~~~~~~~~~~~~~~~~
1102
1138
@@ -1682,6 +1718,7 @@ Learn more
1682
1718
.. _`JMS serializer` : https://github.com/schmittjoh/serializer
1683
1719
.. _RFC3339 : https://tools.ietf.org/html/rfc3339#section-5.8
1684
1720
.. _`options with libxml` : https://www.php.net/manual/en/libxml.constants.php
1721
+ .. _`DOM XML_* constants` : https://www.php.net/manual/en/dom.constants.php
1685
1722
.. _JSON : http://www.json.org/
1686
1723
.. _XML : https://www.w3.org/XML/
1687
1724
.. _YAML : https://yaml.org/
0 commit comments