From 4edb511fe6bfe020e70d6ade1776dd8099a10ed0 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Sat, 26 Jan 2019 19:08:55 +0400 Subject: Sat, 26 Jan 2019 19:08:55 +0400 Signed-off-by: Valentin Popov --- src/main.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f0c9f1..881237d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use serde::Serialize; use serde_json::Result; use xml::reader::{EventReader, XmlEvent}; -#[derive(Serialize)] +#[derive(Serialize, Clone)] struct Record { externalId: String, meetingId: String, @@ -24,32 +24,40 @@ fn main() { let parser = EventReader::new(file); - for e in parser { - match e { + let mut data = Record { + externalId: "".to_string(), + meetingId: "".to_string(), + meetingName: "".to_string(), + }; + + for element in parser { + match element { Ok(XmlEvent::StartElement { name: _, attributes, .. }) => { - for a in attributes { - let mut data: Record; - - match a.name.local_name.as_ref() { - "externalId" => data.externalId = a.value.to_owned(), - "meetingId" => data.meetingId = a.value.to_owned(), - "meetingName" => data.meetingName = a.value.to_owned(), - _ => {} - }; - - let j = serde_json::to_string(&data); - dbg!(j); + for attribute in attributes { + let name: String = attribute.name.local_name.to_string(); + let value: String = attribute.value.to_string(); + + if name == "externalId" { + data.externalId = value; + } else if name == "meetingId" { + data.meetingId = value; + } else if name == "meetingName" { + data.meetingName = value; + } } } - Err(e) => { - dbg!(e); + Err(error) => { + dbg!(error); break; } _ => {} } } + + let j = serde_json::to_string(&data); + dbg!(j); } -- cgit v1.2.3