Sample code for 30+ languages & platforms
Zig

XML SearchForTag Method

See more XML Examples

Demonstrates the SearchForTag and SearchForTag2 methods.

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

    // The sample input XML is available at http://www.chilkatsoft.com/data/fruitSearch.xml
    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";
    const x_search_root = try xml.findChild("searchRoot");
    defer x_search_root.deinit();

    // Search for all nodes having the tag "fruit";
    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.searchForTag(x_search_root, "fruit");
    defer x_found.deinit();
    while (x_search_root.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try x_found.getContent(alloc)});

        x_begin_after = x_found;
        {
            const next = try x_search_root.searchForTag(x_begin_after, "fruit");
            x_found.deinit();
            x_found = next;
        }
    }

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

    // ---------------------------------------------------------------------------------
    // Now do the same, but instead use SearchForTag2
    // 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.searchForTag2(x_begin_after, "fruit")) true else |_| false;
    while (success) {
        std.debug.print("{s}\n", .{try x_search.getContent(alloc)});

        // 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.searchForTag2(x_begin_after, "fruit")) true else |_| false;
    }
}