Sax.SimpleForm (sax v1.0.0) View Source

Provides functions to parse a XML document to simple-form data structure.

Data structure

Simple form is a basic representation of the parsed XML document. It contains a root element, and all elements are in the following format:

element = {tag_name, attributes, content}
content = (element | binary)*

See "Types" section for more information.

Link to this section Summary

Functions

Parse given string into simple form.

Link to this section Types

Specs

attributes() :: [{name :: String.t(), value :: String.t()}]

Specs

content() :: [String.t() | Sax.SimpleForm.t()]

Specs

t() :: {tag_name(), attributes(), content()}

Specs

tag_name() :: String.t()

Link to this section Functions

Link to this function

parse_string(data, options \\ [])

View Source

Specs

parse_string(data :: binary(), options :: Keyword.t()) ::
  {:ok, Sax.SimpleForm.t()} | {:error, exception :: Sax.ParseError.t()}

Parse given string into simple form.

Options

  • :expand_entity - specifies how external entity references should be handled. Three supported strategies respectively are:
    • :keep - keep the original binary, for example Orange ® will be expanded to "Orange ®", this is the default strategy.
    • :skip - skip the original binary, for example Orange ® will be expanded to "Orange ".
    • {mod, fun, args} - take the applied result of the specified MFA.

Examples

Given this XML document.

iex> xml = """
...> <?xml version="1.0" encoding="utf-8" ?>
...> <menu>
...>   <movie url="https://www.imdb.com/title/tt0120338/" id="tt0120338">
...>     <name>Titanic</name>
...>     <characters>Jack &amp; Rose</characters>
...>   </movie>
...>   <movie url="https://www.imdb.com/title/tt0109830/" id="tt0109830">
...>     <name>Forest Gump</name>
...>     <characters>Forest &amp; Jenny</characters>
...>   </movie>
...> </menu>
...> """
iex> Sax.SimpleForm.parse_string(xml)
{:ok,
 {"menu", [],
  [
    "\n  ",
    {
      "movie",
      [
        {"url", "https://www.imdb.com/title/tt0120338/"},
        {"id", "tt0120338"}
      ],
      [
        "\n    ",
        {"name", [], ["Titanic"]},
        "\n    ",
        {"characters", [], ["Jack & Rose"]},
        "\n  "]
    },
    "\n  ",
    {
      "movie",
      [
        {"url", "https://www.imdb.com/title/tt0109830/"},
        {"id", "tt0109830"}
      ],
      [
        "\n    ",
        {"name", [], ["Forest Gump"]},
        "\n    ",
        {"characters", [], ["Forest & Jenny"]},
        "\n  "
      ]
    },
    "\n"
  ]}}