Browse Source

attribute map

Johann Woelper 6 years ago
parent
commit
b45b0a6405
3 changed files with 50 additions and 25 deletions
  1. 17 0
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 31 24
      src/main.rs

+ 17 - 0
Cargo.lock

@@ -136,6 +136,15 @@ dependencies = [
  "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "treexml"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "unicode-xid"
 version = "0.1.0"
@@ -160,11 +169,17 @@ name = "winapi-x86_64-pc-windows-gnu"
 version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
+[[package]]
+name = "xml-rs"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "xmlgear"
 version = "0.1.0"
 dependencies = [
  "quick-xml 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "treexml 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [metadata]
@@ -185,7 +200,9 @@ dependencies = [
 "checksum rustc-demangle 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "adacaae16d02b6ec37fdc7acfcddf365978de76d1983d3ee22afc260e1ca9619"
 "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9"
 "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
+"checksum treexml 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "61b58a77e81f51d427f9cc3a5966211e5be59e5f3c4bfc395babf536595a7609"
 "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
 "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+"checksum xml-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541b12c998c5b56aa2b4e6f18f03664eef9a4fd0a246a55594efae6cc2d964b5"

+ 2 - 1
Cargo.toml

@@ -5,4 +5,5 @@ authors = ["johann.woelper"]
 edition = "2018"
 
 [dependencies]
-quick-xml = "0.13.2"
+quick-xml = "0.13.2"
+treexml = "0.7"

+ 31 - 24
src/main.rs

@@ -1,14 +1,16 @@
 use quick_xml::events::Event;
 use quick_xml::Reader;
+use treexml::Element;
 use std::collections::HashMap;
+use std::str::from_utf8;
 
-#[derive(Debug, Default)]
-struct Element {
-    name: String,
-    attributes: HashMap<String, String>,
-    children: Vec<Element>,
-    text: Option<String>
-}
+// #[derive(Debug, Default)]
+// struct Element {
+//     name: String,
+//     attributes: HashMap<String, String>,
+//     children: Vec<Element>,
+//     text: Option<String>
+// }
 
 
 fn main() {
@@ -30,51 +32,56 @@ fn main() {
     let mut elements = vec![];
     let mut txt = Some("".to_owned());
     let mut buf = Vec::new();
+    let mut attr_map: HashMap<String, String> = HashMap::new();
 
 
 
     // The `Reader` does not implement `Iterator` because it outputs borrowed data (`Cow`s)
     loop {
+
+
         match reader.read_event(&mut buf) {
 
             // elem.text = None;
 
-            // for triggering namespaced events, use this instead:
-            // match reader.read_namespaced_event(&mut buf) {
             Ok(Event::Start(ref e)) => {
-                txt = None;
-
-                // for namespaced:
-                // Ok((ref namespace_value, Event::Start(ref e)))
-                match e.name() {
-                    b"tag1" => println!(
-                        "attributes values: {:?}",
-                        e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>()
-                    ),
-                    b"tag2" => (),
-                    _ => (),
+                for attr in e.attributes(){
+                    match attr {
+                        Ok(a) => {
+                            attr_map.insert(from_utf8(a.key).unwrap_or("").to_owned(), from_utf8(&a.value).unwrap_or("").to_owned());
+                            // dbg!(from_utf8(a.key));
+                        },
+                        Err(_e) => ()
+                    } 
                 }
             },
+            
             // unescape and decode the text event using the reader encoding
             Ok(Event::Text(e)) => {
                 txt = e.unescape_and_decode(&reader).ok();
+            },
 
-                },
             Ok(Event::End(e)) => {
-                let name = std::str::from_utf8(e.name()).unwrap_or("");
+                dbg!(&attr_map);
+                let name = from_utf8(e.name()).unwrap_or("");
                 
                 dbg!(&name);
-
+                // attr_map.insert("ddd".to_owned(),"ddd".to_owned());
                 let elem = Element {
                     name: name.to_owned(),
                     text: txt.clone(),
+                    attributes: attr_map.clone(),
                     ..
                     Default::default()
                 };
 
                 elements.push(elem);
 
-                }
+                txt = None;
+                attr_map = HashMap::new();
+
+
+            }
             Ok(Event::Eof) => break, // exits the loop when reaching end of file
             Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
             _ => (), // There are several other `Event`s we do not consider here