Expand description
Serde-driven XML deserialization.
Map XML directly into typed Rust values:
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq)]
struct Page {
#[serde(rename = "@id")]
id: u32,
title: String,
}
let xml = r#"<page id="7"><title>hello</title></page>"#;
let page: Page = sup_xml::de::from_str(xml).unwrap();
assert_eq!(page, Page { id: 7, title: "hello".into() });§XML → serde conventions
| XML construct | Serde concept | Field name |
|---|---|---|
| Element attribute | struct field | @name |
| Element text-only content | scalar / string field | (the field itself) |
| Element with children | struct | element name |
| Repeated same-name children | Vec<T> | element name |
| Optional element | Option<T> | element name |
| Mixed text + child elements | struct field | $text |
| Heterogeneous element body | enum sequence | $value |
$text collects all text/CDATA between the start and end tag — atomic
content only (primitives, strings, unit enums, space-delimited lists).
$value collects child elements — each one becomes a sequence item,
and for enum types the element tag picks the variant. If both are
declared on the same struct, text routes to $text and remaining child
elements route to $value.
§Borrowed deserialization
Deserializing into &'de str borrows directly from the source. This
works only when all of the following hold for the field’s content:
- The input is UTF-8 (always true for
from_str). - The text contains no entity references (
&, etc.). - The text contains no character references (
&#xNN;). - The text comes from a single
TextorCDataevent — adjacent text and CDATA segments get merged into an ownedStringand lose borrow eligibility.
Use String (or Cow<'_, str>) to handle either case.
§Options
Pass DeOptions to from_str_opts / from_bytes_opts to tune:
the underlying ParseOptions, the magic field names, the attribute
prefix, unknown-field handling, and xsi:nil behaviour.
Structs§
- DeError
- Error returned by the deserializer.
- DeOptions
- Tunables for the deserializer. Defaults match quick-xml’s conventions for drop-in compatibility.
- XmlDeserializer
- Low-level serde XML deserializer.
Functions§
- from_
bytes - Deserialize from an XML byte slice. The bytes must be valid UTF-8.
- from_
bytes_ opts - Like
from_bytes, with caller-suppliedDeOptions. - from_
str - Deserialize a Rust value from an XML string. Borrows from the input where possible (see the borrowed deserialization section for the precise constraints).
- from_
str_ opts - Like
from_str, with caller-suppliedDeOptions.