Browse Source

proper parsing

Johann Woelper 6 years ago
parent
commit
a16fc43363
3 changed files with 46 additions and 21 deletions
  1. 1 1
      .gitignore
  2. 2 0
      Cargo.lock
  3. 43 20
      src/main.rs

+ 1 - 1
.gitignore

@@ -16,6 +16,6 @@
 #Added by cargo
 #
 #already existing elements are commented out
-
+.history
 /target
 **/*.rs.bk

+ 2 - 0
Cargo.lock

@@ -1,3 +1,5 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
 [[package]]
 name = "autocfg"
 version = "0.1.2"

+ 43 - 20
src/main.rs

@@ -12,36 +12,38 @@ struct Element {
 
 
 fn main() {
-    let xml = r#"<tag1 att1 = "testvalue">
-                <tag2><!--Test comment-->Test</tag2>
-                <tag2 att2="othervalue">
-                    text
-                </tag2>
-            </tag1>"#;
+    let xml = r#"
+        <tag0 key = "val"></tag0>
+        <tag1 att1 = "testvalue">
+            <comment><!--Test comment-->Test</comment>
+            <tag2 att2="othervalue">
+                text
+            </tag2>
+            <tex size = "1000"></tex>
+
+        </tag1>
+        "#;
 
     let mut reader = Reader::from_str(xml);
     reader.trim_text(true);
 
-    let mut count = 0;
-    // let mut txt = Vec::new();
+    let mut elements = vec![];
+    let mut txt = Some("".to_owned());
     let mut buf = Vec::new();
 
-    let mut elem = Element {
-        ..
-        Default::default()
-    };
+
 
     // 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;
 
-                let name = std::str::from_utf8(e.name()).unwrap_or("");
-                
-
-                elem.name = name.to_owned();
                 // for namespaced:
                 // Ok((ref namespace_value, Event::Start(ref e)))
                 match e.name() {
@@ -49,19 +51,40 @@ fn main() {
                         "attributes values: {:?}",
                         e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>()
                     ),
-                    b"tag2" => count += 1,
+                    b"tag2" => (),
                     _ => (),
                 }
-            }
+            },
             // unescape and decode the text event using the reader encoding
-            Ok(Event::Text(e)) => elem.text = e.unescape_and_decode(&reader).ok(),
+            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!(&name);
+
+                let elem = Element {
+                    name: name.to_owned(),
+                    text: txt.clone(),
+                    ..
+                    Default::default()
+                };
+
+                elements.push(elem);
+
+                }
             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
         }
 
         // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low
-        dbg!(&elem);
+        
         buf.clear();
     }
+
+
+    dbg!(elements);
 }