4 个版本
新版本 0.2.2 | 2024 年 8 月 16 日 |
---|---|
0.2.1 | 2024 年 8 月 7 日 |
0.2.0 | 2024 年 7 月 27 日 |
0.1.0 | 2024 年 7 月 26 日 |
#2411 在 解析器实现
570 每月下载量
23KB
476 行
ilex
简单的树结构 XML 库。允许读取和写入。
示例
use ilex_xml::{items_to_string, parse_trimmed, Tag, 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().unwrap());
}
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().unwrap();
let color = child.get_attribute("likes").unwrap().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)
);
}
依赖项
~1.5MB
~21K SLoC