serde-xml-fast
A fast, 100% Serde-compatible XML serialization and deserialization library for Rust.
Features
- Full Serde compatibility - Works seamlessly with
#[derive(Serialize, Deserialize)] - High performance - Zero-copy parsing, SIMD-accelerated string operations via
memchr - XML Attributes - First-class support using the
@prefix convention - Rich XML support - CDATA, comments, processing instructions, and more
- Comprehensive error reporting - Line/column positions for all errors
- Minimal dependencies - Only
serde,memchr,itoa, andryu
Performance
| Operation | Throughput |
|---|---|
| Serialization (simple) | ~5.8M structs/sec |
| Serialization (complex) | ~256K structs/sec |
| Deserialization | 190-200 MiB/s |
| XML Parsing | 580+ MiB/s |
| Roundtrip (simple) | ~1.85M ops/sec |
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["derive"] }
Quick Start
use ;
use ;
Examples
Run any example with:
Available examples:
basic- Simple serialization and deserializationnested- Nested struct handlingcollections- Vectors and collectionsattributes- XML attribute support with@prefixhtml_parsing- Parsing HTML-like structures
Nested Structures
use ;
use from_str;
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
use ;
use to_string;
let library = Library ;
let xml = to_string.unwrap;
// <Library><book>Book 1</book><book>Book 2</book></Library>
XML Attributes
Use the @ prefix to serialize/deserialize XML attributes:
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!;
Text Content with Attributes
Use $value or $text to combine attributes with text content:
use ;
use to_string;
let link = Link ;
let xml = to_string.unwrap;
// Output: <Link href="https://example.com">Click here</Link>
HTML-like Parsing
The library can parse well-formed HTML/XHTML structures:
use Deserialize;
use from_str;
let html = r#"
<Form action="/login" method="POST">
<input type="text" name="username"/>
<input type="password" name="password"/>
</Form>
"#;
let form: Form = from_str.unwrap;
assert_eq!;
assert_eq!;
Optional Fields
use ;
use from_str;
let xml = "<Config><name>test</name></Config>";
let config: Config = from_str.unwrap;
assert_eq!;
Automatic Escaping
Special characters are automatically escaped in both content and attributes:
use Serialize;
use to_string;
let elem = Element ;
let xml = to_string.unwrap;
// Attributes: title="Hello "World" & <Friends>"
// Content: <content><script>alert('xss')</script></content>
Low-Level API
For more control, 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
Running Tests
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.