#serde #xml

serde-xml-rs

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

7 releases

0.3.1 Feb 13, 2019
0.3.0 Jan 7, 2019
0.2.1 Aug 17, 2017
0.2.0 Jun 2, 2017
0.1.0 Feb 17, 2017

#27 in Encoding

Download history 2497/week @ 2019-07-28 2856/week @ 2019-08-04 3061/week @ 2019-08-11 2430/week @ 2019-08-18 2497/week @ 2019-08-25 2966/week @ 2019-09-01 2662/week @ 2019-09-08 2992/week @ 2019-09-15 3359/week @ 2019-09-22 5800/week @ 2019-09-29 6329/week @ 2019-10-06 6186/week @ 2019-10-13 4012/week @ 2019-10-20 3577/week @ 2019-10-27 4271/week @ 2019-11-03

12,968 downloads per month
Used in 111 crates (53 directly)

MIT license

39KB
972 lines

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

~500–670KB
~13K SLoC