forked from kudu/xmlread
Little XML parser
- Zig 100%
| src | Upgrade to Zig 0.16.0 | |
| tools | Upgrade to Zig 0.16.0 | |
| xmltest | Initial commit | |
| .gitignore | Initial commit | |
| build.zig | Fix build.zig to work with both zig 0.16 and zig 0.17 | |
| build.zig.zon | Upgrade to Zig 0.16.0 | |
| grammar.txt | Initial commit | |
| LICENSE | Initial commit | |
| README.md | Upgrade to Zig 0.16.0 | |
xmlread
Little XML parser
- XML version 1.0
- encoding: UTF-8, UTF-16, ISO-8859-*
- standalone XML
Installation
-
Add
xmlreaddependency tobuild.zig.zon# for zig-0.16.0 zig fetch --save git+https://codeberg.org/kudu/xmlread#v0.2.0 # for zig-0.15.1 zig fetch --save git+https://codeberg.org/kudu/xmlread#v0.1.0 -
Add to
build.zig:constxmlread=b.dependency("xmlread",.{.target=target,.optimize=optimize,});exe_mod.addImport("xml",xmlread.module("xml"));// Install `xmlread` executableb.installArtifact(xmlread.artifact("xmlread"));
Features
- Pull parser iterating over XML items of a document
(start-tag, end-tag, character data, comment, ...) - Load entire document tree into memory
- Parsed strings are UTF-8 encoded.
Limitations
- Document tree is read-only
Example
zig-0.16.0
conststd=@import("std");constxml=@import("xml");pubfnmain(init:std.process.Init)!void{constgpa=init.gpa;constio=init.io;vardoc=tryxml.tree.load(io,gpa,"test.xml",null);deferdoc.deinit(gpa);constroot=doc.root();std.debug.print("root element name: {s}\n",.{root.name()});std.debug.print("attribute a = {?s}\n",.{root.attr("a")});}zig-0.15.1
conststd=@import("std");constxml=@import("xml");pubfnmain()!void{vargpa_state:std.heap.DebugAllocator(.{})=.init;defer_=gpa_state.deinit();constgpa=gpa_state.allocator();vardoc=tryxml.tree.load(gpa,"test.xml",null);deferdoc.deinit(gpa);constroot=doc.root();std.debug.print("root element name: {s}\n",.{root.name()});std.debug.print("attribute a = {?s}\n",.{root.attr("a")});}see also
- Tests in
src/root.zig