XML to Zig struct

Paste an XML document and zigshape produces Zig structs with xml_root and xml_attribute decoration for serde.zig.

Target

API responseSmallest int widths from observed range; auto enum suggestion; missing/null become optional.

<user id="42">
  <name>Alice</name>
  <role>admin</role>
</user>
XML 5 lines 65 B Drop files here to add samples
User.zig
const serde = @import("serde");

pub const User = struct {
    name: []const u8,
    role: []const u8,
    id: u8,

    pub const serde = .{
        .xml_root = "user",
        .xml_attribute = .{ .id },
    };
};
loading zig fmt 1 struct · 3 fields 12 lines

Field decisions

Why each field got its type. Click a row to see it in the input; edit to override.

struct User $ 3 fields

What zigshape does with XML

  • Attributes are listed in .xml_attribute; child elements become fields.
  • Namespace prefixes stay on the wire: soap:Body becomes a body field with .rename.
  • Text is typed as a number only when it parses back (2.0 is a float, 02134 stays a string).
  • Text inside an element that also has attributes becomes an optional value field, because serde.zig skips that text.

Questions

How do I parse XML in Zig?
With serde.zig: serde.xml.fromSliceManaged(User, allocator, input). The root element is matched against .xml_root.
Are XML namespaces supported?
Prefixed names are kept as the wire names serde.zig matches, and xmlns declarations are ignored rather than turned into fields.