Skip to content

Building & Editing

You can build a document programmatically and serialize it, or edit a parsed tree in place. Mutating methods end in _mut and most return the new node’s NodeId.

use draviavemal_xml_rs::{XmlDocument, XmlAttribute};
let mut document = XmlDocument::new();
let root_id = document.create_root_element_mut(
"root",
Some(vec![XmlAttribute::new("version".into(), "1.0".into())]),
)?;
let child_id = document.append_child_element_mut(
root_id,
"child",
Some(vec![XmlAttribute::new("id".into(), "child1".into())]),
)?;

When element order matters, insert relative to a sibling tag instead of always appending:

MethodInserts the new element…
inser_child_element_after_last_tag_mutafter the last child with the given local tag
inser_child_element_before_first_tag_mutbefore the first child with the given local tag
inser_child_element_after_last_tag_ns_mutafter the last child with the given namespaced tag
inser_child_element_before_first_tag_ns_mutbefore the first child with the given namespaced tag
let id = document.inser_child_element_before_first_tag_mut(
root_id, "header", "body", None,
)?;
let element = document.get_element_mut(child_id)?;
element.add_text_mut("Hello World")?;
element.add_comments_mut("a note")?;
element.add_attribute_mut(XmlAttribute::new("lang".into(), "en".into()))?;
// Remove attributes
element.remove_attribute_mut("lang"); // by local name
element.remove_attribute_ns_mut("t:lang"); // by namespaced name
element.clear_attribute_mut()?; // remove all
use draviavemal_xml_rs::XmlSerializer;
let bytes = XmlSerializer::xml_tree_to_vec(&mut document)?;
// or write straight to disk
XmlSerializer::xml_doc_tree_to_file(&mut document, "out.xml")?;