use fastxml::{Parser, Printer, QueryExt};
fn main() -> fastxml::error::Result<()> {
let xml = r#"<?xml version="1.0"?>
<catalog>
<book id="1">
<title>Original Title</title>
<price>29.99</price>
</book>
<book id="2">
<title>Another Book</title>
<price>19.99</price>
</book>
</catalog>
"#;
let doc = Parser::from(xml.as_bytes()).parse()?;
println!("=== DOM Modification Example ===\n");
let root = doc.get_root_element()?;
println!("Original document:");
println!("{}\n", Printer::from(&root).to_string()?);
println!("1. Adding attribute to root...");
root.set_attribute("version", "1.0");
println!("2. Modifying book title...");
for node in doc.query_nodes("//book[@id='1']/title")? {
node.set_content("Modified Title");
}
println!("3. Adding 'currency' attribute to prices...");
for node in doc.query_nodes("//price")? {
node.set_attribute("currency", "USD");
}
println!("4. Adding discount attribute to all books...");
for node in doc.query_nodes("//book")? {
node.set_attribute("discount", "10%");
}
println!("\nModified document:");
println!("{}", Printer::from(&doc).pretty().to_string()?);
println!("\n5. For element removal, use the Transformer API:");
println!(" See transform_example.rs for examples.");
Ok(())
}