1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mod xml {
use quick_xml;
use quick_xml::events::Event;
use quick_xml::Reader;
pub fn stuff(xml: &str) {
let mut reader = Reader::from_str(xml);
reader.trim_text(true);
let mut count = 0;
let mut txt = Vec::new();
let mut buf = Vec::new();
// The `Reader` does not implement `Iterator` because it outputs borrowed data (`Cow`s)
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) => {
println!(
"Event::Start(ref e) {:?}",
String::from_utf8_lossy(e.name())
);
match e.name() {
b"tag1" => println!(
"attributes values: {:?}",
e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>()
),
b"tag2" => count += 1,
_ => (),
}
}
Ok(Event::Text(e)) => {
println!("Event::Text(e)");
txt.push(e.unescape_and_decode(&reader).unwrap())
}
Ok(Event::Eof) => {
println!("Event::Eof");
break;
} // exits the loop when reaching end of file
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => {
println!("Ignored event");
} // There are several other `Event`s we do not consider here
}
println!("{:?}", txt);
println!("|tag2| = {:?}", count);
println!("");
// if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low
// buf.clear(); // probably wasted since "clear() has no effect on allocated capacity"
}
}
}
fn main() {
xml::stuff(
r#"
<tag1 att1 = "test">
<tag2><!--Test comment-->TEST</tag2>
<tag2>
Test 2
</tag2>
</tag1>
"#,
);
}