READ XML FILE AND GROUP THEM INTO BATCH

 

READ XML FILE AND GROUP THEM INTO BATCH

EXCERCISE: 

  • Reading a XML file and dividing it into seperate chunks

SOLUTION:

  • Create a new binary package called notes:
  • cargo run --build notes
  • Add rust analyser to vscode settings.json.
  • Go to crates.io, search for chrono.
  • Copy their version and paste it in cargo.toml.
  • chrono = "0.4.23"
    xmltree = "0.10.3"
  • Store your xml in dict.xml file

  • use xmltree::Element;
    use std::fs::File;
    use std::io::Read;
    use std::time::{Instant, Duration};
    use std::thread::sleep;
    fn main(){
        let mut file = File::open("dict.xml").unwrap();
        let mut xml_string = String::new();
        file.read_to_string(&mut xml_string).unwrap();
        let start = Instant::now();
        let xml_string = xml_string.replace("", "");
        let root = Element::parse(xml_string.as_bytes()).unwrap();
        let batch_size = 1;
        let children = root.children;
        for (_i, chunk)  in children.chunks(batch_size).enumerate()  {
            let mut root = Element::new("LISTOFLEDGERS");
            root.children = chunk.to_vec();
            sleep(Duration::from_millis(200));
    
        }
        let end = Instant::now();
        let diff = end.duration_since(start);
        println!("The time of execution of above program is {:?}ms", diff.as_millis());
    }
    
    
    
    

Comments