Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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..

    const xml1 = try chilkat.Xml.init();
    defer xml1.deinit();
    xml1.setTag("global");
    xml1.updateAttrAt("phrase", true, "id", "2FADisabled") catch {};
    xml1.updateAttrAt("phrase", true, "value", "2factor authentication disabled") catch {};
    xml1.updateAttrAt("phrase[1]", true, "id", "2FAEnabled") catch {};
    xml1.updateAttrAt("phrase[1]", true, "value", "2factor authentication enabled") catch {};
    xml1.updateAttrAt("phrase[2]", true, "id", "2FAResetFailed") catch {};
    xml1.updateAttrAt("phrase[2]", true, "value", "Failed to disable two factor authentication") catch {};

    const xml2 = try chilkat.Xml.init();
    defer xml2.deinit();
    xml2.setTag("global");
    xml2.updateAttrAt("phrase", true, "id", "2FAResetFailed") catch {};
    xml2.updateAttrAt("phrase", true, "value", "Failed to reset two factor authentication") catch {};
    xml2.updateAttrAt("phrase[1]", true, "id", "2FADisabled") catch {};
    xml2.updateAttrAt("phrase[1]", true, "value", "2FA authentication disabled") catch {};
    xml2.updateAttrAt("phrase[2]", true, "id", "2FAEnabled") catch {};
    xml2.updateAttrAt("phrase[2]", true, "value", "2FA authentication enabled") catch {};

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

    var phrase_id: [:0]const u8 = "";
    var phrase_value: [:0]const u8 = "";

    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();

    var i: i32 = 0;
    const count_i = xml2.numChildrenHavingTag("phrase");
    while (i < count_i) {
        xml2.setI(i);
        phrase_id = try xml2.chilkatPath(alloc, "phrase[i]|(id)");
        phrase_value = try xml2.chilkatPath(alloc, "phrase[i]|(value)");

        // 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"
        sb_path.setString("/A/phrase,id,") catch {};
        sb_path.append(phrase_id) catch {};

        // This assumes the corresponding element exists in xml1.
        xml1.updateAttrAt(try sb_path.getAsString(alloc), true, "value", phrase_value) catch {};

        i = i + 1;
    }

    // Now see how xml1 has been updated..
    std.debug.print("{s}\n", .{try xml1.getXml(alloc)});

    // <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>
}