Sample code for 30+ languages & platforms
Zig

XML SearchForAttribute Method

See more XML Examples

Demonstrates the SearchForAttribute method.

The input XML, available at http://www.chilkatsoft.com/data/fruitSearch.xml, is this:

<root>
    <searchRoot>
        <fruit color="red">apple</fruit>
        <fruit color="green">pear</fruit>
        <veg color="orange">carrot</veg>
        <meat animal="cow">beef</meat>
        <xyz>
            <fruit color="blue">blueberry</fruit>
            <veg color="green">broccoli</veg>
        </xyz>
        <fruit color="purple">grape</fruit>
        <cheese color="yellow">cheddar</cheese>
    </searchRoot>
    <fruit color="red">strawberry</fruit>
    <fruit color="orange">peach</fruit>
</root>

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

    var success: bool = false;

    const xml = try chilkat.Xml.init();
    defer xml.deinit();

    success = if (xml.loadXmlFile("qa_data/xml/fruitSearch.xml")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try xml.getLastErrorText(alloc)});
        return;
    }

    // Search the sub-tree rooted at "searchRoot";
    var x_search_root: chilkat.Xml = try xml.findChild("searchRoot");
    defer x_search_root.deinit();
    if (!xml.getLastMethodSuccess()) {
        std.debug.print("searchRoot not found, searching from root.\n", .{});
        {
            const next = try xml.getRoot();
            x_search_root.deinit();
            x_search_root = next;
        }
    }

    // Search for all "fruit" nodes having a color attribute
    // where the name of the color ends in "e";
    var x_begin_after: chilkat.Xml = try x_search_root.getSelf();
    defer x_begin_after.deinit();
    var x_found: chilkat.Xml = try x_search_root.searchForAttribute(x_begin_after, "fruit", "color", "*e");
    defer x_found.deinit();
    while (x_search_root.getLastMethodSuccess()) {
        std.debug.print("{s}: {s}\n", .{ try x_found.getContent(alloc), try x_found.getAttrValue(alloc, "color") });

        x_begin_after = x_found;
        {
            const next = try x_search_root.searchForAttribute(x_begin_after, "fruit", "color", "*e");
            x_found.deinit();
            x_found = next;
        }
    }

    // The correct output is:
    // grape: purple
    // blueberry: blue

    std.debug.print("--------------------------\n", .{});

    // ---------------------------------------------------------------------------------
    // Now do the same, but instead use SearchForAttribute2
    // which updates the internal reference of the caller instead
    // of returning the found node.

    {
        const next = try x_search_root.getSelf();
        x_begin_after.deinit();
        x_begin_after = next;
    }
    const x_search = try x_search_root.getSelf();
    defer x_search.deinit();

    success = if (x_search.searchForAttribute2(x_begin_after, "fruit", "color", "*e")) true else |_| false;
    while (success) {
        std.debug.print("{s}: {s}\n", .{ try x_search.getContent(alloc), try x_search.getAttrValue(alloc, "color") });

        // Copy the internal references so that the next search
        // begins after the found node.
        x_begin_after.copyRef(x_search);
        x_search.copyRef(x_search_root);

        success = if (x_search.searchForAttribute2(x_begin_after, "fruit", "color", "*e")) true else |_| false;
    }
}