xmlt#
Declarative XML codecs using GADT-based bidirectional maps and bytesrw streaming.
Overview#
Xmlt provides type-safe XML encoding and decoding using a combinator-based approach inspired by the {e finally tagged} pattern from An alphabet for your data soups by Daniel Buenzli. Define a codec once and use it for both encoding and decoding.
Codecs compose from attributes, child elements, and text content using the
El builder module, the same way Jsont composes JSON objects from members.
Streaming I/O is built on bytesrw.
Installation#
opam install xmlt
Usage#
open Xmlt
(* Define a codec for a record type *)
type person = { name : string; age : int; email : string option }
let person =
El.(element "person"
(obj (fun name age email -> { name; age; email })
|> attr "name" Attr.string ~enc:(fun p -> p.name)
|> attr "age" Attr.int ~enc:(fun p -> p.age)
|> child_opt "email" string ~enc:(fun p -> p.email)
|> finish))
(* Decode from XML *)
let () =
match decode_string person "<person name=\"Alice\" age=\"30\"/>" with
| Ok p -> Printf.printf "Name: %s, Age: %d\n" p.name p.age
| Error e -> prerr_endline e
(* Encode to XML *)
let xml = encode_string ~indent:2 person { name = "Bob"; age = 25; email = None }
API Overview#
'a t-- XML codec type (bidirectional)string,int,float,bool-- Text content codecselement-- Wrap a codec in a named XML elementAttrmodule -- Attribute-level codecs (Attr.string,Attr.int,Attr.float,Attr.option)Elmodule -- Element builder:obj,attr,child,child_opt,children,text,finishmap,const,option,list-- Codec combinatorsdecode_string,encode_string-- String I/Odecode,encode-- Streaming I/O via bytesrw readers/writersTreemodule -- Low-level XML tree access (escape hatch)child,attr,nth-- Query combinators for navigating into structures
References#
- Jsont -- The JSON codec library that inspired this approach
- bytesrw -- Composable byte stream readers and writers
Licence#
ISC License. See LICENSE.md for details.