xmlity-quick-xml

Crates.ioxmlity-quick-xml
lib.rsxmlity-quick-xml
version0.0.8
created_at2025-04-09 18:11:39.141181+00
updated_at2025-07-30 10:07:13.210838+00
descriptionXMLity implementation of quick-xml.
homepagehttps://github.com/lukasfri/xmlity
repositoryhttps://github.com/lukasfri/xmlity
max_upload_size
id1627135
size396,460
Lukas Friman (lukasfri)

documentation

https://docs.rs/xmlity

README

XMLity Quick XML   Build Status Latest Version Latest Docs xmlity msrv

This crate contains the implementation of the [quick_xml] backend for XMLity. It is the intention to keep this crate up to date with the latest version of quick-xml and xmlity.

Usage

The easiest way is using the from_str and to_string functions:

use xmlity::{Serialize, Deserialize};;

#[derive(Serialize, Deserialize)]
#[xelement(name = "name")]
struct Name(String);

#[derive(Serialize, Deserialize)]
#[xelement(name = "age")]
struct Age(u8);

#[derive(Serialize, Deserialize)]
#[xelement(name = "person")]
struct Person {
    name: Name,
    age: Age,
}

let person = Person {
    name: Name("John".to_string()),
    age: Age(42),
};

let xml = xmlity_quick_xml::to_string(&person).expect("Failed to serialize");
assert_eq!(xml, r#"<person><name>John</name><age>42</age></person>"#);

let person: Person = xmlity_quick_xml::from_str(&xml).expect("Failed to deserialize");
assert_eq!(person.name.0, "John");
assert_eq!(person.age.0, 42);

But it is also possible to manually create the deserializer and serializer from a quick_xml::NsReader and quick_xml::Writer respectively:

use xmlity::{Serialize, Deserialize};;

#[derive(Serialize, Deserialize)]
#[xelement(name = "single_element")]
struct SingleElement(pub String);

let single_element = SingleElement("Value".to_string());

let serializer = quick_xml::writer::Writer::new(Vec::new());
let mut serializer = xmlity_quick_xml::Serializer::new(serializer);
single_element.serialize(&mut serializer).unwrap();
let bytes = serializer.into_inner();
let xml = String::from_utf8(bytes).unwrap();

assert_eq!(xml, r#"<single_element>Value</single_element>"#);

let mut ns_reader = quick_xml::NsReader::from_reader(xml.as_bytes());
let mut deserializer = xmlity_quick_xml::Deserializer::new(ns_reader);
let single_element = SingleElement::deserialize(&mut deserializer).unwrap();
assert_eq!(single_element.0, "Value");

Creating deserializers and serializers manually gives you more control including modifying namespace prefixes and scopes.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Serde by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Commit count: 76

cargo fmt