exoml v0.0.10 Exoml

A module to decode/encode xml into a tree structure.

The aim of this parser is to be able to represent any xml document as a tree-like structure, but be able to put it back together in a sane way.

In comparison to other xml parsers, this one preserves broken stuff. The goal is to be able to decode the typical broken html document, modify it, and encode it again, without loosing too much of its original content.

Currently the parser preserves whitespace between <xml> nodes, so <pre> or <textarea> tags should be unaffected, by a decode/1 into encode/1.

The only part where the parser tidies up, is the attr="part" of a <xml attr="part"> node.

With well-formed XML, the parser does work really well.

Link to this section Summary


Returns a tree representation from the given xml string.

Returns a string representation from the given xml tree

Link to this section Types

Link to this type


attr() :: binary() | {binary(), binary()}
Link to this type


attr_list() :: [] | [attr()]
Link to this type


opts() :: [{atom(), any()}]
Link to this type


xmlnode() ::
  | {binary(), attr_list(), xmlnode_list()}
  | {binary(), attr_list(), nil}
  | {atom(), attr_list(), nil}
  | {atom(), attr_list(), xmlnode_list()}
Link to this type


xmlnode_list() :: [] | [xmlnode()]
Link to this type


xmlroot() :: {:root, opts(), xmlnode_list()}

Link to this section Functions

Link to this function


decode(binary()) :: xmlroot()

Returns a tree representation from the given xml string.


iex> Exoml.decode("<tag foo=bar>some text<self closing /></tag>")
{:root, [],
 [{"tag", [{"foo", "bar"}],
   ["some text", {"self", [{"closing", "closing"}], nil}]}]}

iex> Exoml.decode("what, this is not xml")
{:root, [], ["what, this is not xml"]}

iex> Exoml.decode("Well, it renders <b>in the browser</b>")
{:root, [], ["Well, it renders ", {"b", [], ["in the browser"]}]}

iex> Exoml.decode("")
{:root, [], []}
Link to this function


encode(xmlroot()) :: binary()

Returns a string representation from the given xml tree

Note when encoding a previously decoded xml document: Exoml does not perfectly preserve everything from a decoded xml document. In general, it preserves just well enough, but don't expect a 1-1 conversion.


iex> xml = ~s'<tag foo="bar">some text</tag>'
iex> ^xml = Exoml.encode(Exoml.decode(xml))
"<tag foo="bar">some text</tag>"