XML to Rust Converter - Generate Rust Structs from XML Online
Convert XML data to Rust structs with serde serialization support from XML documents. Free online tool.
XML to Rust converter, Rust structs, serde serialization, Rust code generation, XML parsing
How to Convert XML to Rust Structs - Step by Step Guide
Add Your XML
Start by adding your XML data that needs to be converted to Rust structs. Paste a real document rather than a schema: the types are worked out from the values it contains, so the more representative it is, the better the structs:
Automatic Rust Struct Generation
The tool automatically analyzes your XML structure and generates Rust structs with serde serialization:
Get Generated Rust Code
The example from the section below comes out as these Rust structs with serde serialization.
Rust Code Output
Your XML becomes these Rust structs:
use serde::{Deserialize, Serialize};
// Field names follow quick-xml's serde conventions ("@name" = attribute, "$text" = text).
// Read a document with: let data: Root = quick_xml::de::from_str(&xml)?;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
#[serde(rename = "@name")]
pub name: Option<String>, // attribute
pub database: Option<Database>, // element
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Database {
pub host: Option<String>, // element
pub port: Option<i32>, // element
pub ssl_enabled: Option<bool>, // element
}Copy or Download Rust Code
Perfect! Now you can use your Rust structs in your applications for XML processing.
Example: Converting Configuration XML to Rust
Let's say you have an application configuration in XML format:
<?xml version="1.0" encoding="UTF-8"?> <config name="app-config"> <database> <host>localhost</host> <port>5432</port> <ssl_enabled>true</ssl_enabled> </database> </config>
After conversion, you'll get Rust structs like this:
use serde::{Deserialize, Serialize};
// Field names follow quick-xml's serde conventions ("@name" = attribute, "$text" = text).
// Read a document with: let data: Root = quick_xml::de::from_str(&xml)?;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Root {
#[serde(rename = "@name")]
pub name: Option<String>, // attribute
pub database: Option<Database>, // element
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Database {
pub host: Option<String>, // element
pub port: Option<i32>, // element
pub ssl_enabled: Option<bool>, // element
}What is XML to Rust Conversion?
XML to Rust conversion transforms XML document structures into Rust structs with serde serialization support. This process analyzes XML elements, attributes, and hierarchical structure to generate memory-safe Rust code with proper type annotations and serialization capabilities.
The generated Rust structs include serde derive macros, optional fields for XML attributes and elements, and nested struct definitions, making it easy to work with XML data in Rust applications with compile-time safety guarantees.
Frequently Asked Questions
Which Rust crates do I need to read XML into these structs?
serde with the derive feature, plus quick-xml with its serialize feature: serde = { version = "1", features = ["derive"] } and quick-xml = { version = "0.37", features = ["serialize"] }. The field renames follow quick-xml's conventions, where "@id" means the id attribute and "$text" means the text inside an element, so a document reads in with quick_xml::de::from_str. Other XML crates name attributes differently and may need the renames adjusted.
How are XML types turned into Rust types?
Every value the converter sees for a field has to fit the type it picks. Whole numbers get i32, or i64/u64 when they are too big for i32, so a 19-digit ID such as 8901240544102066246 stays an exact integer. Decimals like 14.50 become f64, and true/false becomes bool. Anything else stays a String, including values with leading zeros such as 00501, empty values and numbers too long for 64 bits. Every field is an Option because a later document may leave it out.
How does the tool handle nested and repeated elements?
An element with child elements or attributes becomes its own struct, and a child that appears more than once under the same parent becomes a Vec. All copies of an element are merged, so a field that only appears in the second <feature> still ends up in the Feature struct. An element that contains itself, like a tree of <node> elements, is wrapped in a Box so the struct has a known size.
What happens to XML names that are not valid Rust identifiers?
Field names are converted to snake_case and struct names to PascalCase. Names that clash with Rust keywords become raw identifiers such as r#type, names starting with a digit get a prefix, and characters Rust identifiers can't hold are dropped. Whenever the Rust name differs from the XML name, a #[serde(rename = "...")] attribute keeps the original so reading still works. Namespace prefixes are dropped from the rename because quick-xml matches elements by their local name.
Can I use the structs with Actix Web, Axum or Rocket?
Yes, they are plain structs that derive Serialize and Deserialize, so they can be request or response types. Keep in mind those frameworks speak JSON by default, and the renames are written for XML, so an attribute field would show up as "@name" in JSON. For an XML API, read the request body as text and pass it to quick-xml.
Is my XML uploaded anywhere?
No. The conversion runs in your browser, and the XML you paste or upload is not sent to our server. The tool is free to use; very large files can take a moment because the whole document is analysed before any code is written.