Skip to navigation
To parse the European Central Bank (ECB) exchange rate XML using RUST serde-xml-rs
05.01.26
``` [dependencies] serde = { version = "1.0", features = ["derive"] } serde-xml-rs = "0.6.0" ``` ``` use serde::Deserialize; use serde_xml_rs::from_str; #[derive(Debug, Deserialize)] struct Envelope { #[serde(rename = "Cube")] pub cube_root: CubeRoot, } #[derive(Debug, Deserialize)] struct CubeRoot { // This targets the
level #[serde(rename = "Cube")] pub time_entries: Vec
, } #[derive(Debug, Deserialize)] struct TimeEntry { pub time: String, // This targets the
level #[serde(rename = "Cube")] pub rates: Vec
, } #[derive(Debug, Deserialize)] struct RateEntry { pub currency: String, pub rate: f64, } fn main() { let xml_data = r#"
"#; match from_str::
(xml_data) { Ok(envelope) => { for time_entry in envelope.cube_root.time_entries { println!("Date: {}", time_entry.time); for r in time_entry.rates { println!(" {} : {}", r.currency, r.rate); } } } Err(e) => println!("Error: {}", e), } } ```
https://docs.rs/serde-xml-rs/latest/serde_xml_rs/#primitive-types
Reply
Anonymous
Information Epoch 1772351841
Using text data files.
Home
Notebook
Contact us