Sample code for 30+ languages & platforms
Zig

Traverse Direct Children via FirstChild / NextSibling, or LastChild / PreviousSibling

See more XML Examples

Demonstrates some ways to iterate over direct child nodes using the FirstChild / NextSibling and LastChild / PreviousSibling methods.

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

<root>
    <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>
</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/fruit.xml")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try xml.getLastErrorText(alloc)});
        return;
    }

    // Iterate over the direct children by using FirstChild / NextSibling
    var child: chilkat.Xml = try xml.firstChild();
    defer child.deinit();
    var b_continue: bool = xml.getLastMethodSuccess();
    while (b_continue) {
        std.debug.print("{s} : {s}\n", .{ try child.getTag(alloc), try child.getContent(alloc) });
        const next_sibling = try child.nextSibling();
        defer next_sibling.deinit();
        b_continue = child.getLastMethodSuccess();

        child = next_sibling;
    }

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

    // Do the same, but with FirstChild2 / NextSibling2 to avoid
    // creating so many XML object instances:
    success = xml.firstChild2();
    while (success) {
        std.debug.print("{s} : {s}\n", .{ try xml.getTag(alloc), try xml.getContent(alloc) });
        success = xml.nextSibling2();
    }

    // Revert back up to the parent:
    success = if (xml.getParent2()) true else |_| false;

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

    // Iterate in reverse order using LastChild / PreviousSibling
    {
        const next = try xml.lastChild();
        child.deinit();
        child = next;
    }
    b_continue = xml.getLastMethodSuccess();
    while (b_continue) {
        std.debug.print("{s} : {s}\n", .{ try child.getTag(alloc), try child.getContent(alloc) });
        const prev_sibling = try child.previousSibling();
        defer prev_sibling.deinit();
        b_continue = child.getLastMethodSuccess();

        child = prev_sibling;
    }

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

    // Do the same, but with LastChild2 / PreviousSibling2 to avoid
    // creating so many XML object instances:
    success = if (xml.lastChild2()) true else |_| false;
    while (success) {
        std.debug.print("{s} : {s}\n", .{ try xml.getTag(alloc), try xml.getContent(alloc) });
        success = if (xml.previousSibling2()) true else |_| false;
    }

    // Revert back up to the parent:
    success = if (xml.getParent2()) true else |_| false;
}