10个版本
0.0.9 | 2021年5月30日 |
---|---|
0.0.8 | 2021年5月2日 |
0.0.7 | 2021年2月7日 |
0.0.6 | 2021年1月24日 |
0.0.0 | 2020年5月28日 |
#2943 in 解析器实现
用于 wx-sdk
205KB
5.5K SLoC
exile
当前版本:0.0.9
exile
是一个用于读取和写入XML的Rust库。
目标,至少最初,是提供XML文件的抽象语法树。因此,这是一个DOM解析器,它将文档的全部内容加载到内存中。
当前支持
- 属性
- CDATA部分
- 注释解析
- 元素
- 处理指令
- 文本节点
- UTF-8
- 空白字符规范化
不支持
- 文档类型
- 实体
- 实体引用
- 其他编码
- 空白字符保留:所有文本节点都视为空白字符
collapse
处于有效状态。
示例
解析XML看起来像这样。
let xml = r#"
<root>
<thing name="foo"/>
<thing>bar</thing>
</root>
"#;
let doc = exile::parse(xml).unwrap();
for child in doc.root().children() {
println!("element name: {}", child.name());
if let Some(attribute) = child.attribute("name") {
println!("name attribute: {}", attribute);
}
}
// we can create an index of elements
let index = doc.index();
// the element at index 2 is <thing>bar</thing>
let thing = index.element(2).unwrap();
// the parent of index 2 is <root>
let root = index.parent(&thing).unwrap();
assert_eq!("bar", thing.text().unwrap());
assert_eq!("root", root.name());
编写XML看起来像这样。
use exile::{Document, Element, Node};
let mut root = Element::from_name("my_root");
root.add_attribute("foo", "bar");
let mut child = Element::from_name("my_child");
child.add_text("Hello World!");
root.add_child(child);
let doc = Document::from_root(root);
println!("{}", doc.to_string());
上面的程序打印
<my_root foo="bar">
<my_child>Hello World!</my_child>
</my_root>
无运行时依赖
~0–540KB