inline_xml

Created: 2011-07-07 10:10
Updated: 2016-12-20 13:37

README.md

Inline XML Parse Transform Utility

An inline_xml parse transform, based loosely on this.

Usage

Simply include the relevant header file and you're good to go:

-module(testmod).
-compile(export_all).
-include_lib("inline_xml/include/inline_xml.hrl").

demo() ->
    Xml = '<foo><bar name="123" /></foo>',
    {xmlElement,foo,foo,[],
        {xmlNamespace,[],[]},
        [],1,[],
        [{xmlElement,bar,bar,[],
              {xmlNamespace,[],[]},
              [{foo,1}],
              1,
              [{xmlAttribute,name,[],[],[],[],1,[],"123",false}],
              [],[],_,
              undeclared}],
        [],_,undeclared} = Xml.
    io:format("~p~n", [Xml]).

The parse transform will currently handle (i.e., inline) XML (atoms) in each of the following situations:

  1. In an assignment (i.e., match) operation such as X = ''
  2. In a match clause of a case expression

Example use cases are provided in the examples subdirectory.

Alternative XML Libraries

By default, inline_xml uses the standard erlxml library to convert your atom into an AST during the parse_transform. You can tell inline_xml to use another library instead, by defining the alternative API as an M/F/A triple in your module like so:

-module(custom).
-compile(export_all).
-xml_api({erlsom, simple_form, []}).
-include_lib("inline_xml/include/inline_xml.hrl").

demo() ->
    Xml = '<foo attr="baz"><bar>x</bar><bar>y</bar></foo>',
    {"foo",
         [{"attr","baz"}],
         [{"bar",[],["x"]},{"bar",[],["y"]}]} = Xml,
    io:format("~p~n", [Xml]).

Looking into the inline_xml_erlsom module and the inline_erlsom.hrl header in the inline_erlsom extension should give some useful insights into how API customisations and plugins work.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more