|
| 1 | +# Streaming XML |
| 2 | + |
| 3 | +`lazyXml()` reads a large XML file **one element at a time** and hydrates each into a DTO as the collection is consumed. The document is never loaded into memory as a whole — peak memory stays flat whether the file holds a hundred nodes or ten million. |
| 4 | + |
| 5 | +```php |
| 6 | +OfferData::lazyXml($file, 'catalog/shop/offers/offer') |
| 7 | + ->filter(fn (OfferData $o) => $o->available) |
| 8 | + ->chunk(500) |
| 9 | + ->each(fn ($chunk) => Offer::upsert($chunk->map->toArray()->all(), ['id'])); |
| 10 | +``` |
| 11 | + |
| 12 | +The first argument is anything `XMLReader` can open — a path or a stream wrapper such as `compress.zlib://feed.xml.gz`. The second is the slash-separated path of element names, from the root, to the repeated element. |
| 13 | + |
| 14 | +The result is a regular `Illuminate\Support\LazyCollection` of fully typed `OfferData`, so it can be iterated with `foreach`, and `take()`, `filter()`, `chunk()`, `each()` and friends stop reading the file as soon as they have enough. |
| 15 | + |
| 16 | +Requires `ext-xmlreader` (bundled with PHP by default). |
| 17 | + |
| 18 | +## Describing the structure |
| 19 | + |
| 20 | +**One element, one DTO.** A DTO describes exactly one element — its attributes, its text and its direct children. A child that has structure of its own (attributes or children) is a nested DTO, the same way nested data works everywhere else in this package. A property never reaches into a grandchild. |
| 21 | + |
| 22 | +A property reads the child element with its own name. Three attributes say otherwise: |
| 23 | + |
| 24 | +| Attribute | The property reads | |
| 25 | +|---|---| |
| 26 | +| *(none)* | the child element named like the property — its text for a scalar, the whole element for a nested DTO | |
| 27 | +| `#[XmlAttribute]` | the XML attribute named like the property; `#[XmlAttribute('uom')]` for a different name | |
| 28 | +| `#[XmlElement('picture')]` | the child element(s) with that name, when it differs from the property name | |
| 29 | +| `#[XmlText]` | the element's own text — for an element that also carries attributes | |
| 30 | + |
| 31 | +They only affect `lazyXml()`: the same class still hydrates from a plain array under its usual keys, and serializes as usual. See the [attribute reference](../attributes/xml.md). |
| 32 | + |
| 33 | +```xml |
| 34 | +<offer id="42" available="true"> |
| 35 | + <name>Kettle</name> |
| 36 | + <price currency="UAH">499.90</price> |
| 37 | + <picture>https://example.com/a.jpg</picture> |
| 38 | + <picture>https://example.com/b.jpg</picture> |
| 39 | + <param name="Color">Red</param> |
| 40 | + <param name="Volume">1.7 L</param> |
| 41 | + <vendor code="BSH"><name>Bosch</name></vendor> |
| 42 | + <delivery> |
| 43 | + <option cost="80" days="2"/> |
| 44 | + <option cost="0" days="7"/> |
| 45 | + </delivery> |
| 46 | +</offer> |
| 47 | +``` |
| 48 | + |
| 49 | +```php |
| 50 | +class OfferData extends BaseData |
| 51 | +{ |
| 52 | + public function __construct( |
| 53 | + #[XmlAttribute] |
| 54 | + public readonly int $id, |
| 55 | + #[XmlAttribute] |
| 56 | + public readonly bool $available, |
| 57 | + public readonly string $name, |
| 58 | + public readonly PriceData $price, |
| 59 | + #[XmlElement('picture')] |
| 60 | + public readonly array $pictures, |
| 61 | + #[DataCollection(ParamData::class)] |
| 62 | + #[XmlElement('param')] |
| 63 | + public readonly TypedDataCollection $params, |
| 64 | + public readonly ?VendorData $vendor = null, |
| 65 | + public readonly ?DeliveryData $delivery = null, |
| 66 | + ) {} |
| 67 | +} |
| 68 | + |
| 69 | +class PriceData extends BaseData |
| 70 | +{ |
| 71 | + public function __construct( |
| 72 | + #[XmlText] |
| 73 | + public readonly float $amount, |
| 74 | + #[XmlAttribute] |
| 75 | + public readonly string $currency, |
| 76 | + ) {} |
| 77 | +} |
| 78 | + |
| 79 | +class DeliveryData extends BaseData |
| 80 | +{ |
| 81 | + public function __construct( |
| 82 | + #[DataCollection(DeliveryOptionData::class)] |
| 83 | + #[XmlElement('option')] |
| 84 | + public readonly TypedDataCollection $options, |
| 85 | + ) {} |
| 86 | +} |
| 87 | + |
| 88 | +class DeliveryOptionData extends BaseData |
| 89 | +{ |
| 90 | + public function __construct( |
| 91 | + #[XmlAttribute] |
| 92 | + public readonly float $cost, |
| 93 | + #[XmlAttribute] |
| 94 | + public readonly int $days, |
| 95 | + ) {} |
| 96 | +} |
| 97 | + |
| 98 | +class ParamData extends BaseData |
| 99 | +{ |
| 100 | + public function __construct( |
| 101 | + #[XmlAttribute] |
| 102 | + public readonly string $name, |
| 103 | + #[XmlText] |
| 104 | + public readonly string $value, |
| 105 | + ) {} |
| 106 | +} |
| 107 | + |
| 108 | +class VendorData extends BaseData |
| 109 | +{ |
| 110 | + public function __construct( |
| 111 | + #[XmlAttribute] |
| 112 | + public readonly string $code, |
| 113 | + public readonly string $name, |
| 114 | + ) {} |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Mapping rules |
| 119 | + |
| 120 | +- **Scalars.** XML has only strings, so `int`, `float` and `bool` properties (and int-backed enums) are converted from the declared type — no `#[Cast]` needed. `bool` is true for `true` and `1`. A property with a `#[Cast]` receives the raw string. |
| 121 | +- **Empty elements.** `<stock/>` or an empty attribute resolves to `null` when the property is nullable. |
| 122 | +- **Repeated elements.** An `array` property collects the text of every matching element; a `#[DataCollection]` property hydrates every matching element. When there are none, a required list is empty rather than a missing-field error. |
| 123 | +- **Nested DTOs** read a child element with their own mapping, recursively. A wrapper element such as `<delivery>` above is a DTO of its own holding the collection. `#[Flatten]` reads the nested DTO's fields from the parent element. |
| 124 | +- **`#[Discriminator]`** works as usual. The field is read from a child element; to read it from an XML attribute, declare it as an `#[XmlAttribute]` property on the target classes. |
| 125 | +- **Undeclared elements are skipped** without being read into memory, so a DTO that needs three fields of a fifty-field element only pays for three. This also means `#[RejectUnknownKeys]` and class-level pipes only ever see declared fields. |
| 126 | +- **Namespaced elements** are matched by their qualified name as written in the file: `#[XmlElement('g:price')]`. |
| 127 | +- **`#[MapInputName]` and `#[TransformKeys]`** still apply to element names, so a class already mapped for array input needs nothing extra. |
| 128 | + |
| 129 | +## Errors |
| 130 | + |
| 131 | +- A file that cannot be opened, or XML that is malformed or truncated, throws `DataHydrationException`. Elements read before the broken spot are still yielded; a partially read element never is. |
| 132 | +- An element that doesn't satisfy the DTO (a missing required field, an invalid enum value) throws when iteration reaches it, like `lazyCollection()`. |
0 commit comments