expression

XML event-based reading

Process events when building a complete tree is unnecessary. This fixed example counts normal and empty item elements; production input requires additional limits and structural checks. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 2 Common mistake: Counting only Start events and missing self-closing elements.

Formal Syntax & Structure

rust syntaxrust
Reader::from_str; read_event; Event::Eof

Working Example

Example Snippetrust
fn main() {    use quick_xml::{Reader, events::Event};    let mut r = Reader::from_str("<list><item>One</item><item/></list>");    let mut count = 0;    loop {        match r.read_event().unwrap() {            Event::Start(e) | Event::Empty(e) if e.name().as_ref() == b"item" => count += 1,            Event::Eof => break,            _ => {}        }    }    println!("{count}");}
Rust Reference