Hi all!
I’m still fairly new to elixir (mostly Ruby/C#/Rust in my background) and this is my first stab at a semi-useful library so feedback would be much appreciated!
I created a declarative library for creating data mappers that consume XML via strings or streams and return well-defined elixir structs, optionally with default values, attribute extraction, text extraction, and type casting. It’s also fairly fast at what it does based on my benchmarks on some large-ish XML documents. If you’ve used SaxMachine with Ruby it’s almost a 1:1 port to see if I could define a similar interface, since I really like the semantics of the SaxMachine DSL. The general gist is that you describe your expected structure and the library extracts out that information for you without having to specify XPaths or write Sax handlers yourself. This library also tries to be compatible with 3 (currently) XML parsers that support SAX-- erlsom, xmerl_sax_parser, and saxy.
Repo: GitHub - ducharmemp/saxaboom
Hex: saxaboom | Hex
Example:
You can turn:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
Into:
{:ok,
%Catalog{
books: [
%Book{
id: "bk101",
author: "Gambardella, Matthew",
title: "XML Developer's Guide",
genre: "Computer",
price: 44.95,
publish_date: {:ok, ~D[2000-10-01]},
description: "An in-depth look at creating applications\n with XML."
}
]
}}
with the following Saxaboom definition:
defmodule Book do
use Saxaboom.Mapper
document do
element :book, as: :id, value: :id
element :author
element :title
element :genre
element :price, cast: :float
element :publish_date, cast: &__MODULE__.parse_date/1
element :description
end
def parse_date(value), do: Date.from_iso8601(value)
end
defmodule Catalog do
use Saxaboom.Mapper
document do
elements :book, as: :books, into: %Book{}
end
end