Rust
Rust
AddAttribute - Insert New Attribute in an XML Node
Demonstrates adding an name=value attribute to an XML element.
This example uses the XML sample file here: sample1.xml. The sample1.xml file contains this content:
Chilkat Rust Downloads
let xml = chilkat::Xml::new();
if xml.load_xml_file("qa_data/xml/sample1.xml").is_err() {
println!("{}", xml.last_error_text());
return;
}
// Navigate to the "books" element:
if xml.find_child2("books").is_err() {
println!("No books child found!");
return;
}
let num_children = xml.num_children();
let mut i = 0;
while i < num_children {
// Navigate to the Nth book
let _ = xml.get_child2(i);
// Display the book title:
println!("{}", xml.get_attr_value("title").unwrap_or_default());
// Add a new integer attribute:
// Should never fail..
let _ = xml.add_attribute_int("bookId", i);
// Add a new unread="yes" attribute:
let _ = xml.add_attribute("unread", "yes");
// Navigate back up to the parent:
let _ = xml.get_parent2();
i = i + 1;
}
// Navigate back to the document root:
xml.get_root2();
// Save the updated document:
if xml.save_xml("modified.xml").is_err() {
println!("{}", xml.last_error_text());
return;
}
println!("{}", xml.get_xml().unwrap_or_default());