|
@@ -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
|