summaryrefslogtreecommitdiff
path: root/src/xmlelements.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-18 07:23:10 -0500
committertom barrett <spalf0@gmail.com>2019-06-18 07:23:10 -0500
commitc510d4a406218c38e1146503473e9e10c282a56d (patch)
tree07ac460954a6919bb040b70ee791a9fe5473264a /src/xmlelements.rs
parent2ad96b7f882ff962f67a13c7c56a46df86cfe6e3 (diff)
much better way of receiving xml
Diffstat (limited to 'src/xmlelements.rs')
-rw-r--r--src/xmlelements.rs54
1 files changed, 54 insertions, 0 deletions
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<XmlEvent>,
+}
+
+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<usize, ()> {
+ 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(())
+ }
+ }
+}