9 unstable releases (3 breaking)

0.4.2 Oct 1, 2024
0.4.1 Oct 1, 2024
0.4.0 Sep 28, 2024
0.3.1 Sep 9, 2024
0.1.0 Jul 26, 2024

#2899 in Parser implementations

Download history 264/week @ 2024-07-25 123/week @ 2024-08-01 22/week @ 2024-08-08 168/week @ 2024-08-15 3/week @ 2024-08-22 231/week @ 2024-09-05 62/week @ 2024-09-12 25/week @ 2024-09-19 442/week @ 2024-09-26 90/week @ 2024-10-03 15/week @ 2024-10-10 2/week @ 2024-10-17

554 downloads per month

MPL-2.0 license

27KB
500 lines

ilex

crates.io docs

Simple tree structure XML library. Allows reading and writing.

Focus on ease of use while maintaining good performance.

Documentation

Example

use ilex_xml::{items_to_string, parse_trimmed, Item};

let xml = r#"
<!-- The cat is cute. -->
<parent>
    <child likes="orange">Alice</child>
    <child likes="teal">Bob</child>
</parent>
"#;

let mut items = parse_trimmed(xml).unwrap();

{
    // Get comment content
    let Item::Comment(comment) = &items[0] else {
        panic!(
            "Huh, odd. Let's look at the first item's raw XML: {}",
            items[0]
        );
    };

    println!("I found a useful comment:{}", comment.get_value()?);
}

let Item::Element(parent) = &mut items[1] else {
    panic!("Pretty sure the second item is an element.")
};

{
    // Print attributes and text contents of children
    for item in &parent.children {
        let Item::Element(child) = item else {
            panic!("The children are elements, too.")
        };

        let name = child.get_text_content();
        let color = child.get_attribute("likes")?.unwrap();

        println!("{name}'s favorite color is {color}!");
    }
}

println!("Hey, their name isn't Bob! It's Peter!");

{
    // Change child

    // Get child
    let Item::Element(child) = &mut parent.children[1] else {
        panic!();
    };

    // Remove the wrong name
    child.children.pop();
    // Add the correct name
    child.children.push(Item::new_text("Peter"));

    println!(
        "Lets take another look at the raw XML, now that the name is fixed: {}",
        items_to_string(&items)
    );
}

Dependencies

~1.5MB
~21K SLoC