serde-xml
A fast, 100% Serde-compatible XML serialization and deserialization library for Rust.
Features
- Full Serde compatibility - Works with
#[derive(Serialize, Deserialize)] - High performance - Zero-copy parsing, SIMD-accelerated string operations
- Rich XML support - Attributes, namespaces, CDATA, comments, processing instructions
- Comprehensive error reporting - Line/column positions for all errors
- Minimal dependencies - Only
serde,memchr,itoa, andryu
Performance
| Operation | Throughput |
|---|---|
| Serialization (simple) | ~5M structs/sec |
| Deserialization | 180+ MiB/s |
| XML Parsing | 460+ MiB/s |
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
Quick Start
use ;
use ;
Examples
Nested Structures
let xml = r#"
<Person>
<name>Bob</name>
<address>
<city>New York</city>
<country>USA</country>
</address>
</Person>
"#;
let person: Person = from_str.unwrap;
assert_eq!;
Collections
let library = Library ;
let xml = to_string.unwrap;
// <Library><book>Book 1</book><book>Book 2</book></Library>
With Attributes
XML attributes are handled using the @ prefix for field names. Fields without the prefix become child elements:
use ;
use ;
// Serialize with attributes
let item = Item ;
let xml = to_string.unwrap;
// Output: <Item id="123" class="product"><name>Widget</name></Item>
// Deserialize with attributes
let xml = r#"<Item id="456" class="sale"><name>Gadget</name></Item>"#;
let parsed: Item = from_str.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Text Content with Attributes
Use $value or $text to serialize both attributes and text content:
let link = Link ;
let xml = to_string.unwrap;
// Output: <Link href="https://example.com">Click here</Link>
Attribute Escaping
Special characters in attribute values are automatically escaped:
let elem = Element ;
let xml = to_string.unwrap;
// Output: <Element title="Hello "World" & <Friends>"/>
Optional Fields
let xml = "<Config><name>test</name></Config>";
let config: Config = from_str.unwrap;
assert_eq!;
Low-Level API
For more control, you can use the reader and writer directly:
use ;
// Reading
let mut reader = from_str;
while let Ok = reader.next_event
// Writing
let mut writer = new;
writer.start_element.unwrap;
writer.write_text.unwrap;
writer.end_element.unwrap;
Running Benchmarks
License
Copyright 2025 Pegasus Heavy Industries LLC
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.