@@ -1080,23 +1080,59 @@ These are the options available:
10801080============================== ================================================= ==========================
10811081Option Description Default
10821082============================== ================================================= ==========================
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
10851085``xml_version `` Sets the XML version attribute ``1.1 ``
10861086``xml_encoding `` Sets the XML encoding attribute ``utf-8 ``
10871087``xml_standalone `` Adds standalone attribute in the generated XML ``true ``
10881088``xml_type_cast_attributes `` This provides the ability to forgot the attribute ``true ``
10891089 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 ``
10921092 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
10951097``load_options `` XML loading `options with libxml `_ ``\LIBXML_NONET | \LIBXML_NOBLANKS ``
10961098``remove_empty_tags `` If set to true, removes all empty tags in the ``false ``
10971099 generated XML
10981100============================== ================================================= ==========================
10991101
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+
11001136The ``YamlEncoder ``
11011137~~~~~~~~~~~~~~~~~~~
11021138
@@ -1682,6 +1718,7 @@ Learn more
16821718.. _`JMS serializer` : https://github.com/schmittjoh/serializer
16831719.. _RFC3339 : https://tools.ietf.org/html/rfc3339#section-5.8
16841720.. _`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
16851722.. _JSON : http://www.json.org/
16861723.. _XML : https://www.w3.org/XML/
16871724.. _YAML : https://yaml.org/
0 commit comments