4 releases

0.1.10 Aug 10, 2020
0.1.8 Aug 10, 2020
0.1.7 Aug 10, 2020
0.1.6 Aug 7, 2020

#2585 in Rust patterns

Download history 137/week @ 2024-03-11 103/week @ 2024-03-18 138/week @ 2024-03-25 113/week @ 2024-04-01 90/week @ 2024-04-08 59/week @ 2024-04-15 125/week @ 2024-04-22 165/week @ 2024-04-29 137/week @ 2024-05-06 86/week @ 2024-05-13 107/week @ 2024-05-20 172/week @ 2024-05-27 113/week @ 2024-06-03 111/week @ 2024-06-10 105/week @ 2024-06-17 164/week @ 2024-06-24

495 downloads per month

Custom license

18KB
384 lines

Simple XML

Simple xml is a small crate for reading, parsing and storing xml data

Usage

Example parsing:


let note =
    simple_xml::from_file("./examples/note.xml").expect("Failed to parse simple_xml");

let to = &note["to"][0];
let from = &note["from"][0];
let heading = &note.get_nodes("heading").expect("Missing heading")[0];
let body = &note["body"][0];
let lang = note
    .get_attribute("lang")
    .expect("Failed to get attribute lang");

More examples can be found in the docs and tests


lib.rs:

XML parser and writer This crate can load xml from a file or string and parse it into memory XML can also be manipulated or created and the written to file

Loading xml from a file

fn load_message() -> Result<(), simple_xml::Error> {
    let root = simple_xml::from_file("examples/message.xml")?;
    // Since there can multiple nodes/tags with the same name, we need to index twice
    let heading = &root["heading"][0];
    println!("Heading: {}", heading.content);
    // Access attributes
    let lang = root.get_attribute("lang").expect("Missing lang attribute");
    println!("Language: {}", lang);
    Ok(())
}

Creating xml structures

let name = String::from("Tim Roberts");
let health = 50;

let mut player = simple_xml::new("player", String::new());
player.add_new_node("health", health.to_string());
player.add_new_node("name", name);
// Save to file
player.save_to_file("./player.xml");

For more example, see the tests

No runtime deps