Rust:
Working with XML

How to:

In Rust, you can handle XML with crates like xml-rs. Install by adding xml-rs = "0.8" to your Cargo.toml. Here’s how to parse a simple XML:

extern crate xml;

use xml::reader::{EventReader, XmlEvent};

fn main() {
    let xml_data = r#"<book category="fiction">
    <title>Rust in Action</title>
    <author>Tim McNamara</author>
    <year>2021</year>
</book>"#;

    let parser = EventReader::from_str(xml_data);
    for e in parser {
        match e {
            Ok(XmlEvent::StartElement { name, .. }) => {
                println!("Start: {}", name);
            }
            Ok(XmlEvent::Characters(data)) => {
                println!("Text: {}", data);
            }
            Ok(XmlEvent::EndElement { name }) => {
                println!("End: {}", name);
            }
            Err(e) => {
                println!("Error: {}", e);
            }
            _ => {}
        }
    }
}

Output:

Start: book
Start: title
Text: Rust in Action
End: title
Start: author
Text: Tim McNamara
End: author
Start: year
Text: 2021
End: year
End: book

This code stream-reads XML, handling start and end elements plus text data, logging each step.

Deep Dive:

XML’s a senior in tech years, crafted for the web in the late 90s. Its design promotes readability (for both machines and humans) and extensive self-describing data.

Alternatives? Sure, JSON is the modern go-to for web APIs, lighter and less noisy. Meanwhile, YAML’s picked up fans for configs, with its clean layout. But XML’s not going anywhere soon—vast infrastructures are built on its back.

Under the hood, Rust’s XML parsing leans on iterator patterns, keeping memory usage low and performance sharp. You’ll find crates like serde-xml-rs for a more serde-like experience—a boon for those used to JSON handling.

See Also:

For more on Rust and XML: