Sample code for 30+ languages & platforms
Zig

Demonstrate the XML UpdateAttrAt Method

See more XML Examples

Demonstrates the XML UpdateAttrAt method to update attribute name/value pairs in XML.

Note: This example requires Chilkat v9.5.0.64 or later.

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();

    // Note: This example requires Chilkat v9.5.0.64 or later.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();

    xml.setTag("animals");

    const auto_create = true;

    // When the autoCreate argument equals true, the UpdateAttrAt
    // method auto-creates the children as needed:
    xml.updateAttrAt("penguin", auto_create, "feature", "flightless bird") catch {};
    xml.updateAttrAt("mammal|rodent|squirrel", auto_create, "feature", "bushy tail") catch {};
    xml.updateAttrAt("mammal|rodent|rat", auto_create, "feature", "long hairless tail") catch {};

    // Show the XML
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    //     <?xml version="1.0" encoding="utf-8" ?>
    //     <animals>
    //         <penguin feature="flightless bird" />
    //         <mammal>
    //             <rodent>
    //                 <squirrel feature="bushy tail" />
    //                 <rat feature="long hairless tail" />
    //             </rodent>
    //         </mammal>
    //     </animals>
    //

    // Now update each node's "feature" attribute..
    xml.updateAttrAt("penguin", auto_create, "feature", "black and white") catch {};
    xml.updateAttrAt("mammal|rodent|squirrel", auto_create, "feature", "grey, red, or black") catch {};
    xml.updateAttrAt("mammal|rodent|rat", auto_create, "feature", "brown, grey, white, or black") catch {};

    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    //     <?xml version="1.0" encoding="utf-8" ?>
    //     <animals>
    //         <penguin feature="black and white" />
    //         <mammal>
    //             <rodent>
    //                 <squirrel feature="grey, red, or black" />
    //                 <rat feature="brown, grey, white, or black" />
    //             </rodent>
    //         </mammal>
    //     </animals>
    //
}