XML to Zig struct
Paste an XML document and zigshape produces Zig structs with xml_root and xml_attribute decoration for serde.zig.
API responseSmallest int widths from observed range; auto enum suggestion; missing/null become optional.
<user id="42"> <name>Alice</name> <role>admin</role> </user>
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 },
};
};
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:Bodybecomes abodyfield with.rename. - Text is typed as a number only when it parses back (
2.0is a float,02134stays a string). - Text inside an element that also has attributes becomes an optional
valuefield, 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
xmlnsdeclarations are ignored rather than turned into fields.