-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from efx/example-nested
doc: example of nested parsing
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
extern crate quick_xml; | ||
use quick_xml::events::Event; | ||
use quick_xml::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(&mut buf)? { | ||
Event::Start(element) => match element.name() { | ||
b"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(&mut skip_buf)? { | ||
Event::Start(element) => match element.name() { | ||
b"w:tr" => { | ||
stats.rows.push(vec![]); | ||
row_index = stats.rows.len() - 1; | ||
} | ||
b"w:tc" => { | ||
stats.rows[row_index] | ||
.push(String::from_utf8(element.name().to_vec()).unwrap()); | ||
} | ||
_ => {} | ||
}, | ||
Event::End(element) => { | ||
if element.name() == b"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].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].rows.len(), 2); | ||
assert_eq!(found_tables[1].rows[0].len(), 4); | ||
assert_eq!(found_tables[1].rows[1].len(), 4); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14"><w:body><w:tbl><w:tblPr><w:tblW w:w="9972" w:type="dxa"/><w:jc w:val="left"/><w:tblInd w:w="0" w:type="dxa"/><w:tblBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tblBorders><w:tblCellMar><w:top w:w="55" w:type="dxa"/><w:left w:w="53" w:type="dxa"/><w:bottom w:w="55" w:type="dxa"/><w:right w:w="55" w:type="dxa"/></w:tblCellMar></w:tblPr><w:tblGrid><w:gridCol w:w="2493"/><w:gridCol w:w="2493"/><w:gridCol w:w="2493"/><w:gridCol w:w="2492"/></w:tblGrid><w:tr><w:trPr></w:trPr><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>A</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>B</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>C</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2492" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:right w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>D</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:trPr></w:trPr><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>E</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>F</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>G</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2492" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:right w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>H</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p><w:pPr><w:sectPr><w:type w:val="nextPage"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:left="1134" w:right="1134" w:header="0" w:top="1134" w:footer="0" w:bottom="1134" w:gutter="0"/><w:pgNumType w:fmt="decimal"/><w:formProt w:val="false"/><w:textDirection w:val="lrTb"/><w:docGrid w:type="default" w:linePitch="100" w:charSpace="0"/></w:sectPr><w:pStyle w:val="Normal"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr></w:r></w:p><w:tbl><w:tblPr><w:tblW w:w="9972" w:type="dxa"/><w:jc w:val="left"/><w:tblInd w:w="0" w:type="dxa"/><w:tblBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tblBorders><w:tblCellMar><w:top w:w="55" w:type="dxa"/><w:left w:w="53" w:type="dxa"/><w:bottom w:w="55" w:type="dxa"/><w:right w:w="55" w:type="dxa"/></w:tblCellMar></w:tblPr><w:tblGrid><w:gridCol w:w="2493"/><w:gridCol w:w="2493"/><w:gridCol w:w="2493"/><w:gridCol w:w="2492"/></w:tblGrid><w:tr><w:trPr></w:trPr><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>A</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>B</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>C</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2492" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:right w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>D</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:trPr></w:trPr><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>E</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>F</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2493" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>G</w:t></w:r></w:p></w:tc><w:tc><w:tcPr><w:tcW w:w="2492" w:type="dxa"/><w:tcBorders><w:top w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:left w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:bottom w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:right w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideH w:val="single" w:sz="2" w:space="0" w:color="000000"/><w:insideV w:val="single" w:sz="2" w:space="0" w:color="000000"/></w:tcBorders><w:shd w:fill="auto" w:val="clear"/></w:tcPr><w:p><w:pPr><w:pStyle w:val="TableContents"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr><w:t>H</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr></w:rPr></w:pPr><w:r><w:rPr></w:rPr></w:r></w:p><w:sectPr><w:type w:val="nextPage"/><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:left="1134" w:right="1134" w:header="0" w:top="1134" w:footer="0" w:bottom="1134" w:gutter="0"/><w:pgNumType w:fmt="decimal"/><w:formProt w:val="false"/><w:textDirection w:val="lrTb"/><w:docGrid w:type="default" w:linePitch="100" w:charSpace="0"/></w:sectPr></w:body></w:document> |