XML Parsing

xml-rs

xml-website xml xml-crates.io xml-github xml-lib.rs cat-parser-implementations

xml is an XML library written in pure Rust.

// // COMING SOON

quick-xml

quick-xml quick-xml-crates.io quick-xml-github quick-xml-lib.rs cat-asynchronous cat-encoding cat-parser-implementations cat-parsing

quick-xml is a high-performance XML reader and writer. quick_xml is fast for streaming.

// // COMING SOON

xmlparser

xmlparser xmlparser-crates.io xmlparser-github xmlparser-lib.rs cat-parser-implementations

xmlparser is a pull-based, zero-allocation XML parser.

// // COMING SOON

xml5ever

xml5ever-website xml5ever xml5ever-crates.io xml5ever-github xml5ever-lib.rs cat-parser-implementations cat-web-programming

xml5ever is a push-based streaming parser for XML.

// // COMING SOON

Parse XML as a read-only tree with roxmltree

roxmltree roxmltree-crates.io roxmltree-github roxmltree-lib.rs cat-parser-implementations

roxmltree represents an XML as a read-only tree. It is good for simple parsing.

//! This example demonstrates how to parse an XML document using the `roxmltree`
//! crate.
//!
//! The `roxmltree` crate provides a read-only tree representation of an XML
//! document. It's a good choice for parsing XML when you don't need to modify
//! the document.
//!
//! Built on top of this API, a mapping to the Serde data model is available via
//! the serde-roxmltree crate.
//!
//! This example parses a simple XML document representing a library of books.
use roxmltree::Document;

fn main() -> anyhow::Result<()> {
    let xml = r#"<?xml version="1.0" encoding="UTF-8"?><library><book id="1"><title>The Rust Programming Language</title><author>Steve Klabnik</author><author>Carol Nichols</author><year>2018</year><categories><category>Programming</category><category>Rust</category></categories></book></library>"#;

    // Parse the XML.
    let doc = Document::parse(xml)?;

    // Get the root element.
    let root = doc.root_element();
    println!("Root element: {}", root.tag_name().name());

    // Iterate through all books.
    for book in root.children().filter(|n| n.has_tag_name("book")) {
        println!("\nBook ID: {}", book.attribute("id").unwrap_or("unknown"));

        // Get the title.
        if let Some(title) = book.children().find(|n| n.has_tag_name("title")) {
            println!("Title: {}", title.text().unwrap_or(""));
        }

        // Get all authors.
        let authors: Vec<&str> = book
            .children()
            .filter(|n| n.has_tag_name("author"))
            .filter_map(|n| n.text())
            .collect();

        println!("Authors: {}", authors.join(", "));

        // Get the year.
        if let Some(year) = book.children().find(|n| n.has_tag_name("year")) {
            println!("Year: {}", year.text().unwrap_or(""));
        }

        // Get categories.
        if let Some(categories) =
            book.children().find(|n| n.has_tag_name("categories"))
        {
            let category_list: Vec<&str> = categories
                .children()
                .filter(|n| n.has_tag_name("category"))
                .filter_map(|n| n.text())
                .collect();

            println!("Categories: {}", category_list.join(", "));
        }
    }
    Ok(())
}

Other Options

minidom builds a DOM tree.