For processing very large TEI documents without loading them entirely into
memory, the streaming feature enables incremental parsing via a pull-parser
interface.
Enabling the feature
Add the streaming feature to the tei-xml dependency:
[dependencies]
tei-xml = { version = "0.1", features = ["streaming"] }
Usage
The TeiPullParser implements Iterator, yielding TeiEvent values as it
processes the document:
use std::io::BufReader;
use std::fs::File;
use tei_xml::streaming::{TeiPullParser, TeiEvent};
fn process_tei(path: &str) -> Result<(), Box<dyn std::error::Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let parser = TeiPullParser::new(reader);
for event in parser {
match event? {
TeiEvent::DocumentStart => println!("Parsing started"),
TeiEvent::Header(header) => {
println!("Title: {}", header.file_desc().title().as_str());
}
TeiEvent::BodyBlock(block) => println!("Received block: {block:?}"),
TeiEvent::DocumentEnd => println!("Parsing complete"),
}
}
Ok(())
}
For string slices, use the convenience constructor:
let parser = TeiPullParser::from_str(xml_string);
Event types
The parser yields four high-level event types:
DocumentStart: Emitted once at the beginning of parsingHeader(TeiHeader): The complete header metadata, emitted once after the header section is fully parsedBodyBlock(BodyBlock): A paragraph, utterance, or division from the body, emitted one at a time as each block is parsed. Division blocks are accumulated with their full child content (lists, items, nested paragraphs and utterances) before being yielded as a singleBodyBlock::DiveventDocumentEnd: Emitted once after all content has been successfully parsed
Parser state overview
This diagram shows the main InBody to InDiv flow and the return paths from
InHead, InParagraph, and InUtterance.
stores heading on parent div
restores parent div
InBody
InDiv
InHead
InParagraph
InUtterance
Full state coverage, including list, item, and label states, is documented in the design document.
The streaming parser currently streams the header and body only. Root-level
<standOff> markup is supported by full-document parsing and emission, but it
is not yet emitted as a streaming event.