Crates.io | exile |
lib.rs | exile |
version | 0.0.9 |
source | src |
created_at | 2020-05-28 02:00:37.962503 |
updated_at | 2021-05-30 04:43:58.306668 |
description | DOM-Style XML Parser |
homepage | |
repository | https://github.com/webern/exile/ |
max_upload_size | |
id | 246891 |
size | 212,479 |
Current version: 0.0.9
exile
is a Rust library for reading and writing XML.
The goal, at least initially, is to provide an abstract syntax tree of an XML file. As such, this is a Exile is a dom parser and loads the complete contents of the document into memory.
Currently supported:
Not Supported:
collapse
were in-effect.Parsing XML looks like this.
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());
Authoring XML looks like this.
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());
The above program prints:
<my_root foo="bar">
<my_child>Hello World!</my_child>
</my_root>