4 releases
new 0.0.3 | May 6, 2025 |
---|---|
0.0.2 | May 2, 2025 |
0.0.1 | Apr 16, 2025 |
0.0.0 | Apr 9, 2025 |
#1782 in Encoding
447 downloads per month
190KB
4.5K
SLoC
# XMLity Quick XML

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.
Dependencies
~1.4–2MB
~37K SLoC