{"record":{"id":"0a54b9fd5e1243a9","repo":"cakephp/cakephp","slug":"xml-serialization-of-view-data-failed","errorCode":null,"errorMessage":"XML serialization of View data failed.","messagePattern":"XML serialization of View data failed\\.","errorType":"exception","errorClass":"SerializationFailureException","httpStatus":null,"severity":"error","filePath":"src/View/XmlView.php","lineNumber":157,"sourceCode":"                $data !== null &&\n                (!is_array($data) || Hash::numeric(array_keys($data)))\n            ) {\n                $data = [$rootNode => [$serialize => $data]];\n            }\n        }\n\n        $options = $this->getConfig('xmlOptions', []);\n        if (Configure::read('debug')) {\n            $options['pretty'] = true;\n        }\n\n        /**\n         * @var array<mixed> $data\n         * @var string|false $result\n         */\n        $result = Xml::fromArray($data, $options)->saveXML();\n        if ($result === false) {\n            throw new SerializationFailureException(\n                'XML serialization of View data failed.',\n            );\n        }\n\n        return $result;\n    }\n}\n","sourceCodeStart":139,"sourceCodeEnd":165,"githubUrl":"https://github.com/cakephp/cakephp/blob/1128eba9b09f1946df684811350e26f2cef68fac/src/View/XmlView.php#L139-L165","documentation":"XmlView renders a response as XML by converting the view's serialized data via Xml::fromArray() and calling saveXML(). If saveXML() returns false, the DOM document could not be serialized (structurally invalid XML document), and CakePHP throws SerializationFailureException. This is an internal-invariant check: fromArray already validates array shape, so false indicates an unexpected low-level serialization failure.","triggerScenarios":"A controller using static::serializationType('xml') / RequestHandler with XML extension where the data array contains structures Xml::fromArray cannot turn into a valid DOM document (e.g. invalid element names, mixed/nested content that yields an unsaveable document).","commonSituations":"Serving API endpoints as XML where entity data contains keys that are invalid XML tag names (spaces, leading digits, special chars) or deeply inconsistent arrays; users hitting a URL with the .xml extension on data intended for JSON.","solutions":["Inspect the data passed to the view (set with $this->set(...)) and ensure top-level keys and array shapes are XML-serializable via Xml::fromArray().","Sanitize array keys to valid XML element names (letters/underscores, no spaces or invalid chars) before setting view data.","Add a manual Xml::fromArray($data)->saveXML() check in development to see the underlying DOM error.","If XML output is not actually required, remove the .xml extension usage or restrict serialization types to json."],"exampleFix":"// before\n$this->set('data', $rows);\n$this->viewBuilder()->setClassName('Cake.View.Xml');\n// after\n$this->set('data', array_map(function ($r) {\n    unset($r['invalid key!']); // or rename to valid element name\n    return $r;\n}, $rows));\n$this->viewBuilder()->setClassName('Cake.View.Xml');","handlingStrategy":"try-catch","validationCode":"use Cake\\Utility\\Xml;\ntry {\n    Xml::fromArray($data)->saveXML();\n} catch (\\Throwable $e) {\n    // keys/shape not XML-serializable — sanitize before rendering\n}","typeGuard":"function isXmlSerializable(array $data): bool {\n    foreach (array_keys($data) as $k) {\n        if (!is_string($k) || !preg_match('/^[A-Za-z_][A-Za-z0-9_.-]*$/', $k)) {\n            return false;\n        }\n    }\n    return true;\n}","tryCatchPattern":"try {\n    $this->render('xml');\n} catch (\\Cake\\View\\Exception\\SerializationFailureException $e) {\n    return $this->getResponse()->withStatus(500)\n        ->withStringBody('XML serialization failed');\n}","preventionTips":["Keep view data arrays simple: string keys that are valid XML element names","Smoke-test XML responses in CI by requesting .xml versions of API endpoints","Prefer JSON unless XML is a hard client requirement"],"tags":["cakephp","xml","serialization","view"],"backgroundTag":"json-serialization-failed","analyzedSha":"1128eba9b09f1946df684811350e26f2cef68fac","analyzedAt":"2026-09-12T12:07:00.388Z","contentChangedAt":"2026-09-12T12:07:00.388Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}