#serde #xml

serde-xml-rs

xml-rs based deserializer for Serde (compatible with 0.9+)

9 releases

Uses old Rust 2015

0.4.1 Jan 10, 2021
0.4.0 Mar 11, 2020
0.3.1 Feb 13, 2019
0.3.0 Jan 7, 2019
0.1.0 Feb 17, 2017
Download history 12593/week @ 2021-04-03 12967/week @ 2021-04-10 16480/week @ 2021-04-17 22138/week @ 2021-04-24 20693/week @ 2021-05-01 20747/week @ 2021-05-08 23046/week @ 2021-05-15 25963/week @ 2021-05-22 22798/week @ 2021-05-29 20976/week @ 2021-06-05 21324/week @ 2021-06-12 21055/week @ 2021-06-19 20686/week @ 2021-06-26 19968/week @ 2021-07-03 23193/week @ 2021-07-10 19825/week @ 2021-07-17

68,792 downloads per month
Used in 256 crates (107 directly)

MIT license

40KB
1K SLoC

serde-xml-rs

Build Status

xml-rs based deserializer for Serde (compatible with 0.9+)

Usage

Use serde_xml_rs::from_reader(...) on any type that implements std::io::Read as following:

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_xml_rs;

use serde_xml_rs::from_reader;

#[derive(Debug, Deserialize)]
struct Item {
    pub name: String,
    pub source: String
}

#[derive(Debug, Deserialize)]
struct Project {
    pub name: String,

    #[serde(rename = "Item", default)]
    pub items: Vec<Item>
}

fn main() {
    let s = r##"
        <Project name="my_project">
            <Item name="hello" source="world.rs" />
        </Project>
    "##;
    let project: Project = from_reader(s.as_bytes()).unwrap();
    println!("{:#?}", project);
}

Alternatively, you can use serde_xml_rs::Deserializer to create a deserializer from a preconfigured xml_rs::EventReader.

Parsing the "value" of a tag

If you have an input of the form <foo abc="xyz">bar</foo>, and you want to get at thebar, you can use the special name $value:

struct Foo {
    pub abc: String,
    #[serde(rename = "$value")]
    pub body: String,
}

Parsed representations

Deserializer tries to be as intuitive as possible.

However, there are some edge cases where you might get unexpected errors, so it's best to check out tests for expectations.

Dependencies

~0.7–1.3MB
~30K SLoC