From c510d4a406218c38e1146503473e9e10c282a56d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 18 Jun 2019 07:23:10 -0500 Subject: much better way of receiving xml --- src/xmlelements.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/xmlelements.rs (limited to 'src/xmlelements.rs') diff --git a/src/xmlelements.rs b/src/xmlelements.rs new file mode 100644 index 0000000..86d7c77 --- /dev/null +++ b/src/xmlelements.rs @@ -0,0 +1,54 @@ +use ggez::filesystem::File; +use std::io::BufReader; +use xml::reader::{ + EventReader, + XmlEvent::{self, StartElement}, +}; + +pub struct XMLElements { + pub events: Vec, +} + +impl XMLElements { + pub fn new(file: File) -> XMLElements { + XMLElements { + events: EventReader::new(BufReader::new(file)) + .into_iter() + .map(Result::unwrap) + .collect(), + } + } + + pub fn get_element(&self, element_name: &str) -> XmlEvent { + self.events + .clone() + .into_iter() + .find(|e| { + if let StartElement { name, .. } = e { + name.local_name == element_name + } else { + false + } + }) + .unwrap() + } + + pub fn get_element_attribute( + &self, + element_name: &str, + attribute_name: &str, + ) -> Result { + let element = self.get_element(element_name); + if let StartElement { attributes, .. } = element { + Ok(attributes + .iter() + .find(|a| a.name.local_name == attribute_name) + .unwrap() + .value + .parse() + .unwrap()) + } else { + Err(()) + } + } +} -- cgit v1.2.3