Sample code for 30+ languages & platforms
Rust

Find and Update an XML Attribute Value

See more XML Examples

Demonstrates how to find an element in XML with a specified attribute name and then update the attribute value.

Chilkat Rust Downloads

Rust
// Let's say you have 2 XML files with identical structures as shown directly below,
// and you wish to update the values in the 1st XML file with those in the 2nd XML file.

// <global>
//     <phrase id="2FADisabled" value="2factor authentication disabled"/>
//     <phrase id="2FAEnabled" value="2factor authentication enabled"/>
//     <phrase id="2FAResetFailed" value="Failed to disable two factor authentication"/>
// </global>

// <global>
//     <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/>
//     <phrase id="2FADisabled" value="2FA authentication disabled"/>
//     <phrase id="2FAEnabled" value="2FA authentication enabled"/>
// </global>

// First, initialize each of the Chilkat XML objects.
// These could be loaded from files, but we'll just create them manually for this example..

let xml1 = chilkat::Xml::new();
xml1.set_tag("global");
let _ = xml1.update_attr_at("phrase", true, "id", "2FADisabled");
let _ = xml1.update_attr_at("phrase", true, "value", "2factor authentication disabled");
let _ = xml1.update_attr_at("phrase[1]", true, "id", "2FAEnabled");
let _ = xml1.update_attr_at("phrase[1]", true, "value", "2factor authentication enabled");
let _ = xml1.update_attr_at("phrase[2]", true, "id", "2FAResetFailed");
let _ = xml1.update_attr_at("phrase[2]", true, "value", "Failed to disable two factor authentication");

let xml2 = chilkat::Xml::new();
xml2.set_tag("global");
let _ = xml2.update_attr_at("phrase", true, "id", "2FAResetFailed");
let _ = xml2.update_attr_at("phrase", true, "value", "Failed to reset two factor authentication");
let _ = xml2.update_attr_at("phrase[1]", true, "id", "2FADisabled");
let _ = xml2.update_attr_at("phrase[1]", true, "value", "2FA authentication disabled");
let _ = xml2.update_attr_at("phrase[2]", true, "id", "2FAEnabled");
let _ = xml2.update_attr_at("phrase[2]", true, "value", "2FA authentication enabled");

// Iterate over attribute values in xml2 and update the same in xml1.

let mut phrase_id = String::new();
let mut phrase_value = String::new();

let sb_path = chilkat::StringBuilder::new();

let mut i = 0;
let count_i = xml2.num_children_having_tag("phrase");
while i < count_i {
    xml2.set_i(i);
    phrase_id = xml2.chilkat_path("phrase[i]|(id)").unwrap_or_default();
    phrase_value = xml2.chilkat_path("phrase[i]|(value)").unwrap_or_default();

    // Notice how the UpdateAttrAt method's 1st argument is a Chilkat path -- which is an expression
    // to find the location of the XML element to be updated.
    // Here we create a path of the form "/A/tagName,attributeName,attributeValuePattern", which means
    // find the direct descendent of xml2 having tag=tagName, with an attribute=attributeName where the attribute value
    // matches the attributeValuePattern.  If attributeValuePattern does not contain a "*", then it must match directly.
    // We'll build a Chilkat path such as "/A/phrase,id,2FADisabled"
    let _ = sb_path.set_string("/A/phrase,id,");
    let _ = sb_path.append(&phrase_id);

    // This assumes the corresponding element exists in xml1.
    let _ = xml1.update_attr_at(&sb_path.get_as_string().unwrap_or_default(), true, "value", &phrase_value);

    i = i + 1;
}

// Now see how xml1 has been updated..
println!("{}", xml1.get_xml().unwrap_or_default());

// <global>
//     <phrase id="2FADisabled" value="2FA authentication disabled"/>
//     <phrase id="2FAEnabled" value="2FA authentication enabled"/>
//     <phrase id="2FAResetFailed" value="Failed to reset two factor authentication"/>
// </global>