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