Minimal dependency-free XML parser and serializer
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remove accidentally committed bearssl C files

These were picked up by git add -A but are not part of
ocaml-crypto (they cause build failures).

+70
+70
README.md
··· 1 + # xmlt 2 + 3 + Declarative XML codecs using GADT-based bidirectional maps and bytesrw streaming. 4 + 5 + ## Overview 6 + 7 + Xmlt provides type-safe XML encoding and decoding using a combinator-based 8 + approach inspired by the {e finally tagged} pattern from 9 + [An alphabet for your data soups](https://github.com/dbuenzli/jsont/blob/main/paper/soup.tex) 10 + by Daniel Buenzli. Define a codec once and use it for both encoding and decoding. 11 + 12 + Codecs compose from attributes, child elements, and text content using the 13 + `El` builder module, the same way Jsont composes JSON objects from members. 14 + Streaming I/O is built on [bytesrw](https://github.com/dbuenzli/bytesrw). 15 + 16 + ## Installation 17 + 18 + ``` 19 + opam install xmlt 20 + ``` 21 + 22 + ## Usage 23 + 24 + ```ocaml 25 + open Xmlt 26 + 27 + (* Define a codec for a record type *) 28 + type person = { name : string; age : int; email : string option } 29 + 30 + let person = 31 + El.(element "person" 32 + (obj (fun name age email -> { name; age; email }) 33 + |> attr "name" Attr.string ~enc:(fun p -> p.name) 34 + |> attr "age" Attr.int ~enc:(fun p -> p.age) 35 + |> child_opt "email" string ~enc:(fun p -> p.email) 36 + |> finish)) 37 + 38 + (* Decode from XML *) 39 + let () = 40 + match decode_string person "<person name=\"Alice\" age=\"30\"/>" with 41 + | Ok p -> Printf.printf "Name: %s, Age: %d\n" p.name p.age 42 + | Error e -> prerr_endline e 43 + 44 + (* Encode to XML *) 45 + let xml = encode_string ~indent:2 person { name = "Bob"; age = 25; email = None } 46 + ``` 47 + 48 + ## API Overview 49 + 50 + - **`'a t`** -- XML codec type (bidirectional) 51 + - **`string`**, **`int`**, **`float`**, **`bool`** -- Text content codecs 52 + - **`element`** -- Wrap a codec in a named XML element 53 + - **`Attr`** module -- Attribute-level codecs (`Attr.string`, `Attr.int`, `Attr.float`, `Attr.option`) 54 + - **`El`** module -- Element builder: `obj`, `attr`, `child`, `child_opt`, `children`, `text`, `finish` 55 + - **`map`**, **`const`**, **`option`**, **`list`** -- Codec combinators 56 + - **`decode_string`**, **`encode_string`** -- String I/O 57 + - **`decode`**, **`encode`** -- Streaming I/O via bytesrw readers/writers 58 + - **`Tree`** module -- Low-level XML tree access (escape hatch) 59 + - **`child`**, **`attr`**, **`nth`** -- Query combinators for navigating into structures 60 + 61 + ## References 62 + 63 + - [Jsont](https://github.com/dbuenzli/jsont) -- The JSON codec library that 64 + inspired this approach 65 + - [bytesrw](https://github.com/dbuenzli/bytesrw) -- Composable byte stream 66 + readers and writers 67 + 68 + ## Licence 69 + 70 + ISC License. See [LICENSE.md](LICENSE.md) for details.