roundtrip/
roundtrip.rs

1// RustyXML
2// Copyright 2013-2016 RustyXML developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10extern crate xml;
11use std::fs::File;
12use std::io::Read;
13
14fn main() {
15    let mut args = std::env::args();
16    let name = args.next().unwrap_or_else(|| "roundtrip".to_string());
17    let path = args.next();
18    let path = if let Some(ref path) = path {
19        path
20    } else {
21        println!("Usage: {} <file>", name);
22        return;
23    };
24    let mut rdr = match File::open(path) {
25        Ok(file) => file,
26        Err(err) => {
27            println!("Couldn't open file: {}", err);
28            std::process::exit(1);
29        }
30    };
31
32    let mut p = xml::Parser::new();
33    let mut e = xml::ElementBuilder::new();
34
35    let mut string = String::new();
36    if let Err(err) = rdr.read_to_string(&mut string) {
37        println!("Reading failed: {}", err);
38        std::process::exit(1);
39    };
40
41    p.feed_str(&string);
42    for event in p.filter_map(|x| e.handle_event(x)) {
43        // println!("{:?}", event);
44        match event {
45            Ok(e) => println!("{}", e),
46            Err(e) => println!("{}", e),
47        }
48    }
49}