Import serde-xml-rs 0.4.1 am: 1e046a8595 am: ecb08a10cc am: fc2bfac46b am: aa251e43eb

Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/serde-xml-rs/+/1767647

Change-Id: Id697721de05be6bf7d29ff4b1886574eade3ec9f
tree: 33bde6c515a7bd7b50de72cedb31a280a14d37a1
  1. src/
  2. tests/
  3. Android.bp
  4. Cargo.toml
  5. cargo2android.json
  6. LICENSE
  7. METADATA
  8. MODULE_LICENSE_MIT
  9. OWNERS
  10. README.md
  11. rustfmt.toml
README.md

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.