Repository navigation
Expand file tree
/
Copy pathnested_readers.rs
More file actions
76 lines (74 loc) · 2.8 KB
/
Copy pathnested_readers.rs
File metadata and controls
76 lines (74 loc) · 2.8 KB
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
67
68
69
70
71
72
73
74
75
76
use pretty_assertions::assert_eq;
use quick_xml::events::Event;
use quick_xml::reader::Reader;
// a structure to capture the rows we've extracted
// from a ECMA-376 table in document.xml
#[derive(Debug, Clone)]
struct TableStat {
index: u8,
rows: Vec<Vec<String>>,
}
// demonstrate how to nest readers
// This is useful for when you need to traverse
// a few levels of a document to extract things.
fn main() -> Result<(), quick_xml::Error> {
let mut buf = Vec::new();
// buffer for nested reader
let mut skip_buf = Vec::new();
let mut count = 0;
let mut reader = Reader::from_file("tests/documents/document.xml")?;
let mut found_tables = Vec::new();
loop {
match reader.read_event_into(&mut buf)? {
Event::Start(element) => {
if element.name().as_ref() == "w:tbl" {
count += 1;
let mut stats = TableStat {
index: count,
rows: vec![],
};
// must define stateful variables
// outside the nested loop else they are overwritten
let mut row_index = 0;
loop {
skip_buf.clear();
match reader.read_event_into(&mut skip_buf)? {
Event::Start(element) => match element.name().as_ref() {
"w:tr" => {
stats.rows.push(vec![]);
row_index = stats.rows.len() - 1;
}
"w:tc" => {
stats.rows[row_index].push(element.name().as_ref().to_string());
}
_ => {}
},
Event::End(element) => {
if element.name().as_ref() == "w:tbl" {
found_tables.push(stats);
break;
}
}
_ => {}
}
}
}
}
Event::Eof => break,
_ => {}
}
buf.clear();
}
assert_eq!(found_tables.len(), 2);
// pretty print the table
println!("{:#?}", found_tables);
assert_eq!(found_tables[0].index, 1);
assert_eq!(found_tables[0].rows.len(), 2);
assert_eq!(found_tables[0].rows[0].len(), 4);
assert_eq!(found_tables[0].rows[1].len(), 4);
assert_eq!(found_tables[1].index, 2);
assert_eq!(found_tables[1].rows.len(), 2);
assert_eq!(found_tables[1].rows[0].len(), 4);
assert_eq!(found_tables[1].rows[1].len(), 4);
Ok(())
}