Rust-Etree

Rust-Etree on Travis CI

XML DOM library with XPath support.

Usage

Put this in your Cargo.toml:

toml [dependencies] etree = "0.1"

```rust use etree::*; use std::path::Path;

fn createxml>(path:P) { let mut tree:ETree = ETree::from(ETreeNode::new("ROOT")); tree.setencoding("UTF-8"); tree.setstandalone("no"); let rootpos = tree.root();

// append first child
let mut child1:ETreeNode = ETreeNode::new("CHILD-A");
child1.set_attr("DEST", "CHN");
let child1_pos = tree.append_child_node(root_pos, child1).unwrap();
// append another child after first child
let mut child2:ETreeNode = ETreeNode::new("CHILD-B");
child2.set_text("Shanghail");
tree.append_next_node(child1_pos, child2);
// append another child before first child
let mut child3:ETreeNode = ETreeNode::new("CHILD-C");
child3.set_attr("DEST", "CHN");
child3.set_text("Shanghail");
tree.append_previous_node(child1_pos, child3);
// append a child in the first child
let mut child4:ETreeNode = ETreeNode::new("SUBCHILD-A");
child4.set_text("EAST");
let pos = tree.find("//CHILD-A").unwrap(); // after inserting child3, child1_pos becomes invaild
tree.append_child_node(pos, child4);
tree.pretty("\n  ");
tree.write_file(path).ok();

}

fn modifyxml>(pathin:P, pathout:P) { let mut tree = ETree::parsefile(pathin); let subtreepos = tree.find("//CHILD-A").unwrap(); let mut subtree = tree.subtree(subtreepos); let subtreechildpos = subtree.find("/SUBCHILD-A").unwrap(); if let Some(node) = subtree.nodemut(subtreechildpos) { node.settext("WEST"); } // tree.appendnexttree(subtreepos, subtree.clone()); let parentpos = tree.parent(subtreepos).unwrap(); tree.appendchildtree(parentpos, subtree); tree.writefile(path_out).ok(); } ```

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.