Sample code for 30+ languages & platforms
Zig

Auto-Trim XML Content when Loading

See more XML Examples

This example explains the "autoTrim" argument that is passed to a method such as LoadXml2.

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

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

    // If autoTrim is true, then the content inside an leaf element is trimmed.
    // For example:
    var auto_trim: bool = true;
    xml.loadXml2("<abc><xyz>  123   </xyz></abc>", auto_trim) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Output is:
    // (notice the SPACE chars before and after "xyz" are trimmed)

    // <?xml version="1.0" encoding="utf-8" ?>
    // <abc>
    //     <xyz>123</xyz>
    // </abc>

    // If autoTrim is false, then the content inside leaf elements are not trimmed.
    auto_trim = false;
    xml.loadXml2("<abc><xyz>  123   </xyz></abc>", auto_trim) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Output is:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <abc>
    //     <xyz>  123   </xyz>
    // </abc>

    // --------------------------------------------------------------------
    // The EmitCompact property controls whether XML is emitted indented (pretty-printed)
    // or compact.  For example:

    // Auto-trim + emit compact:
    auto_trim = true;
    xml.loadXml2("<abc><xyz>  123   </xyz></abc>", auto_trim) catch {};
    xml.setEmitCompact(true);
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Output is:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <abc><xyz>123</xyz></abc>

    // No Auto-trim + emit compact:
    auto_trim = false;
    xml.loadXml2("<abc><xyz>  123   </xyz></abc>", auto_trim) catch {};
    xml.setEmitCompact(true);
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Output is:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <abc><xyz>  123   </xyz></abc>
}