Skip to main content

Module de

Module de 

Source
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 constructSerde conceptField name
Element attributestruct field@name
Element text-only contentscalar / string field(the field itself)
Element with childrenstructelement name
Repeated same-name childrenVec<T>element name
Optional elementOption<T>element name
Mixed text + child elementsstruct field$text
Heterogeneous element bodyenum 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:

  1. The input is UTF-8 (always true for from_str).
  2. The text contains no entity references (&amp;, etc.).
  3. The text contains no character references (&#xNN;).
  4. The text comes from a single Text or CData event — adjacent text and CDATA segments get merged into an owned String and 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-supplied DeOptions.
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-supplied DeOptions.