Advertisement728 × 90 — Leaderboard
Advertisement320 × 50 — Mobile banner

XML to Rust Serde

Generate Rust structs with serde from XML
Session restored from your last visit
Also need:XML to JSONJSON to RustXML Formatter
Advertisement — Fullscreen
Input
Output
Input
AdvertisementResponsive
Output
Ready0 chars0 lines
Advertisement728 × 90

About XML to Rust Serde

Paste your XML and get Rust struct definitions with #[derive(Serialize, Deserialize)] and serde rename attributes for XML attributes.

Frequently Asked Questions

What is XML to Rust struct generation?
XML to Rust struct generation analyses an XML document and produces Rust struct definitions with serde and quick-xml derive macros. The structs can be used with quick-xml's serde-based deserialiser to parse XML into typed Rust data structures.
What Rust crates can I use to deserialize XML into the generated struct?
Add quick-xml = { version = "0.31", features = ["serialize"] } and serde = { version = "1", features = ["derive"] } to Cargo.toml. Then annotate the struct with #[derive(Deserialize)] and use quick_xml::de::from_str(&xml_string). Alternatively, use serde-xml-rs for a slightly different attribute handling convention.
How are XML attributes handled in Rust serde deserialization?
With quick-xml's serde support, attribute fields are annotated with #[serde(rename = "@attr_name")]. The @-prefix convention distinguishes attributes from child elements. The generator outputs these annotations. Verify that attribute names match the XML source exactly, including any namespace prefixes.
How do I handle optional XML elements in a Rust struct?
Wrap the field type in Option<T>: pub name: Option<String>. With #[serde(default)], a missing element sets the field to None. For text content of an element, use a #[serde(rename = "$text")] annotated field. For empty optional elements, the Option<String> field returns Some("") — add a custom deserializer to map empty strings to None if needed.
Can I use the generated Rust struct to both read and write XML?
Yes. Add #[derive(Serialize, Deserialize)] to get both directions. Serialising back to XML: quick_xml::se::to_string(&my_struct). The output preserves element names and attribute annotations from the struct. For pretty-printed XML, write to a quick_xml::Writer configured with indentation.
Advertisement300 × 250
Advertisement300 × 250
Ad
AdvertisementSticky banner
Ad
AdvertisementSticky banner