Turns XML DOM tree into XML string. cf. inner_xml()
use amxml::dom::*;
let xml_string = r#"<?xml version="1.0"?><article>About <em>XML</em> string</article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
let result = root_elem.to_string();
assert_eq!(result, "<article>About <em>XML</em> string</article>");
Turns XML DOM tree into 'pretty' XML string with four spaces indent.
use amxml::dom::*;
let xml_string = r#"<?xml version="1.0"?><article>About <em>XML</em> string</article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
let result = root_elem.to_pretty_string();
let guess = r#"<article>
About
<em>
XML
</em>
string
</article>
"#;
assert_eq!(result, guess);
Turns XML DOM tree under self into XML string. cf. to_string()
use amxml::dom::*;
let xml_string = r#"<?xml version="1.0"?><article>About <em>XML</em> string</article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
let result = root_elem.inner_xml();
assert_eq!(result, "About <em>XML</em> string");
Returns type of the node (NodeType::Element, etc.).
Returns the name of the Element/Attribute node,
or the target of the Instruction node.
use amxml::dom::*;
let xml_string = r#"<ns:article>XML</ns:article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
assert_eq!(root_elem.name(), "ns:article");
Returns the value of the Attribute node,
text of the Text/Comment node,
inst of XMLDecl/Instruction node.
use amxml::dom::*;
let xml_string = r#"<ns:article>XML</ns:article>"#;
let doc = new_document(&xml_string).unwrap();
let text_node = doc.get_first_node("//text()").unwrap();
assert_eq!(text_node.value(), "XML");
Returns the local name of Element.
use amxml::dom::*;
let xml_string = r#"<ns:article>foo</ns:article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
assert_eq!(root_elem.local_name(), "article");
Returns the space name of Element, or "" if not with namespace.
use amxml::dom::*;
let xml_string = r#"<ns:article>foo</ns:article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
assert_eq!(root_elem.space_name(), "ns");
Returns the URI for namespace.
use amxml::dom::*;
let xml_string = r#"<root xmlns="http://def" xmlns:ns="http://ns"><ns:a/><b/></root>"#;
let doc = new_document(&xml_string).unwrap();
let root = doc.root_element();
let elem_ns_a = doc.get_first_node("//ns:a").unwrap();
let elem_b = doc.get_first_node("//b").unwrap();
assert_eq!(elem_ns_a.namespace_uri(), "http://ns");
assert_eq!(elem_b.namespace_uri(), "http://def");
Returns the root (topmost) node of DOM tree.
use amxml::dom::*;
let xml_string = r#"<article><p>DOM</p></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_p = doc.get_first_node("/article/p").unwrap();
assert_eq!(elem_p.name(), "p");
let root = elem_p.root();
assert_eq!(root.node_type(), NodeType::DocumentRoot);
Returns the topmost Element node,
or root node when there is no Element node (illegal case),
use amxml::dom::*;
let xml_string = r#"<article><p>DOM</p></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_p = doc.get_first_node("/article/p").unwrap();
assert_eq!(elem_p.name(), "p");
let root_elem = elem_p.root_element();
assert_eq!(root_elem.name(), "article");
Returns the parent of the 'node', or None
if 'node' has no parent (i.e. is DocumentRoot).
use amxml::dom::*;
let xml_string = r#"<article><chapter>foo</chapter></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_chapter = doc.get_first_node("//chapter").unwrap();
let p = elem_chapter.parent().unwrap();
assert_eq!(p.name(), "article");
Returns the first child of the node.
This is equivalent to: nth_child(0)
Returns the n'th child of the node, or None if there is
no such child.
use amxml::dom::*;
let xml_string = r#"<article><a/>foo<b/></article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
assert_eq!(root_elem.name(), "article");
let ch0 = root_elem.first_child().unwrap();
assert_eq!(ch0.name(), "a");
let ch1 = root_elem.nth_child(1).unwrap();
assert_eq!(ch1.value(), "foo");
let ch2 = root_elem.nth_child(2).unwrap();
assert_eq!(ch2.name(), "b");
let ch3 = root_elem.nth_child(3);
assert!(ch3.is_none());
Appends the node tree 'new_child' as the last child of
the element node.
use amxml::dom::*;
let xml_string = r#"<article><a/>foo<b/></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_article = doc.get_first_node("//article").unwrap();
let new_xml_string = r#"<c>baa</c>"#;
let new_doc = new_document(&new_xml_string).unwrap();
let elem_c = new_doc.root_element();
elem_article.append_child(&elem_c);
let result = doc.to_string();
let guess = r#"<article><a/>foo<b/><c>baa</c></article>"#;
assert_eq!(result, guess);
Inserts the child node tree as the previous sibling of 'self' node.
use amxml::dom::*;
let xml_string = r#"<article><a/><b/><c/></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_b = doc.get_first_node("//b").unwrap();
let new_xml_string = r#"<x>yyy</x>"#;
let new_doc = new_document(&new_xml_string).unwrap();
let elem_x = new_doc.root_element();
elem_b.insert_as_previous_sibling(&elem_x);
let result = doc.to_string();
let guess = r#"<article><a/><x>yyy</x><b/><c/></article>"#;
assert_eq!(result, guess);
Inserts the child node tree as the next sibling of 'self' node.
use amxml::dom::*;
let xml_string = r#"<article><a/><b/><c/></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_b = doc.get_first_node("//b").unwrap();
let new_xml_string = r#"<x>yyy</x>"#;
let new_doc = new_document(&new_xml_string).unwrap();
let elem_x = new_doc.root_element();
elem_b.insert_as_next_sibling(&elem_x);
let result = doc.to_string();
let guess = r#"<article><a/><b/><x>yyy</x><c/></article>"#;
assert_eq!(result, guess);
Deletes the child node tree from 'self' node.
use amxml::dom::*;
let xml_string = r#"<article><a/><b/><c/><d/></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_article = doc.get_first_node("//article").unwrap();
let elem_b = elem_article.get_first_node("b").unwrap();
elem_article.delete_child(&elem_b);
let result = doc.to_string();
let guess = r#"<article><a/><c/><d/></article>"#;
assert_eq!(result, guess);
Replaces the child node tree with 'self' node.
use amxml::dom::*;
let xml_string = r#"<article><a/><b/><c/></article>"#;
let doc = new_document(&xml_string).unwrap();
let elem_b = doc.get_first_node("//b").unwrap();
let new_xml_string = r#"<x>yyy</x>"#;
let new_doc = new_document(&new_xml_string).unwrap();
let elem_x = new_doc.root_element();
elem_b.replace_with(&elem_x);
let result = doc.to_string();
let guess = r#"<article><a/><x>yyy</x><c/></article>"#;
assert_eq!(result, guess);
Returns the attribute value of element,
or None if there is no such attribute.
use amxml::dom::*;
let xml_string = r#"<article id="a1">foo</article>"#;
let doc = new_document(&xml_string).unwrap();
let root_elem = doc.root_element();
assert_eq!(root_elem.attribute_value("id").unwrap(), "a1");
assert!(root_elem.attribute_value("none").is_none());
Updates the attribute value (if already exists) of element,
or adds the attribute (if not exist).
use amxml::dom::*;
let xml_string = r#"<article id="a1">foo</article>"#;
let doc = new_document(&xml_string).unwrap();
let mut root_elem = doc.root_element();
assert_eq!(root_elem.attribute_value("id").unwrap(), "a1");
root_elem.set_attribute("id", "b1");
assert_eq!(root_elem.attribute_value("id").unwrap(), "b1");
root_elem.set_attribute("title", "about xml");
assert_eq!(root_elem.attribute_value("title").unwrap(), "about xml");
assert_eq!(doc.to_string(), r#"<article id="b1" title="about xml">foo</article>"#);
Deletes the attribute (if already exists) of element.
use amxml::dom::*;
let xml_string = r#"<article id="a1">foo</article>"#;
let doc = new_document(&xml_string).unwrap();
let mut root_elem = doc.root_element();
root_elem.delete_attribute("id");
assert!(root_elem.attribute_value("id").is_none());
assert_eq!(doc.to_string(), r#"<article>foo</article>"#);
(Inner Use) Returns node_id.
Evaluates the xpath and returns the sequence.
use amxml::dom::*;
let xml = r#"<root><a v="x"/><a v="y"/></root>"#;
let doc = new_document(xml).unwrap();
let result = doc.eval_xpath(r#"some $a in /root/a satisfies $a/@v = "y" "#).unwrap();
assert_eq!(result.to_string(), "true");
- When syntax error or unimplemented feature in xpath.
Retrieves the first node that match with xpath.
Returns None if not found,
or when syntax error or unimplemented feature in xpath.
use amxml::dom::*;
let xml = r#"<root img="basic"><a img="a1"/><a img="a2"/></root>"#;
let doc = new_document(xml).unwrap();
let node = doc.get_first_node("//a").unwrap();
assert_eq!(node.attribute_value("img").unwrap(), "a1");
- When syntax error or unimplemented feature in xpath.
Applies func to each node that match with xpath.
use amxml::dom::*;
let xml = r#"<root img="basic"><a img="a1"/><a img="a2"/></root>"#;
let doc = new_document(xml).unwrap();
let mut img = String::new();
doc.each_node("/root/a", |n| {
img += n.attribute_value("img").unwrap().as_str();
});
assert_eq!("a1a2", img);
- When syntax error or unimplemented feature in xpath.
Retrieves all nodes that match with xpath in document order.
use amxml::dom::*;
let xml = r#"<root img="basic"><a img="a1"/><a img="a2"/></root>"#;
let doc = new_document(xml).unwrap();
let nodeset = doc.get_nodeset("//a").unwrap();
assert_eq!(nodeset[0].attribute_value("img").unwrap(), "a1");
assert_eq!(nodeset[1].attribute_value("img").unwrap(), "a2");
- When syntax error or unimplemented feature in xpath.