> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/serialization/xml.md).

# XML Serialization

XML serialization mirrors the JSON API. The XML root element name is set to the FHIR resource type (e.g. `Patient`), and the FHIR namespace `http://hl7.org/fhir` is emitted by default.

```php
<?php

use Ardenexal\FHIRTools\Component\Models\R4B\Resource\PatientResource;

$patient = new PatientResource(id: 'example-123', active: true);

$xml = $serializer->serializeToXml($patient);
// <?xml version="1.0"?>
// <Patient xmlns="http://hl7.org/fhir"><id value="example-123"/><active value="true"/></Patient>

$restored = $serializer->deserializeFromXml($xml, PatientResource::class);
echo $restored->id; // "example-123"
```

The signatures are `serializeToXml(object $fhirObject, array $context = []): string` and `deserializeFromXml(string $xmlData, string $targetClass, array $context = []): object`. See [Context & Options](/serialization/context.md) for the shared options.

{% hint style="warning" %}
**XXE protection.** XML deserialization strips `DOCTYPE` declarations (`XmlEncoder::DECODER_IGNORED_NODE_TYPES` set to `XML_DOCUMENT_TYPE_NODE`) so external entity definitions are never processed. Attribute values are also preserved as strings (`TYPE_CAST_ATTRIBUTES` disabled) so numeric-looking primitives such as `"1.0"` or `"2002"` keep their precision on round-trip rather than being cast to float/int.
{% endhint %}

## Error handling

Like JSON, all failures are wrapped in `FHIRSerializationException`:

```php
<?php

use Ardenexal\FHIRTools\Component\Serialization\Exception\FHIRSerializationException;

try {
    $patient = $serializer->deserializeFromXml($maybeInvalidXml, PatientResource::class);
} catch (FHIRSerializationException $e) {
    error_log('FHIR XML deserialization failed: ' . $e->getMessage());
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php-fhir-tools.ardenexal.net/serialization/xml.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
