Saxy v0.2.0-rc1 Saxy View Source

Saxy is a XML SAX parser which provides functions to parse XML file in both binary and streaming way. Comply with Extensible Markup Language (XML) 1.0 (Fifth Edition).

Link to this section Summary

Link to this section Functions

Link to this function parse_stream(stream, handler, state) View Source
parse_stream(
  stream :: File.Stream.t() | Stream.t(),
  handler :: module() | function(),
  state :: term()
) :: {:ok, state :: term()} | {:error, exception :: Saxy.ParsingError.t()}

Parses XML stream data.

This function takes a stream, SAX event handler (see more at Saxy.Handler) and an initial state as the input, it returns {:ok, state} if parsing is successful, otherwise {:error, exception}, where exception is a Saxy.ParsingError struct which can be converted into readable message with Exception.message/1.

Examples

defmodule MyEventHandler do
  @behaviour Saxy.Handler

  def handle_event(:start_document, prolog, state) do
    IO.inspect "Start parsing document"
    [{:start_document, prolog} | state]
  end

  def handle_event(:end_document, _data, state) do
    IO.inspect "Finish parsing document"
    [{:end_document} | state]
  end

  def handle_event(:start_element, {name, attributes}, state) do
    IO.inspect "Start parsing element #{name} with attributes #{inspect(attributes)}"
    [{:start_element, name, attributes} | state]
  end

  def handle_event(:end_element, {name}, state) do
    IO.inspect "Finish parsing element #{name}"
    [{:end_element, name} | state]
  end

  def handle_event(:characters, chars, state) do
    IO.inspect "Receive characters #{chars}"
    [{:chacters, chars} | state]
  end
end

iex> stream = File.stream!("/path/to/file.xml")
iex> Saxy.parse_stream(stream, MyEventHandler, [])
{:ok,
 [
   {:end_document},
   {:end_element, "foo"},
   {:start_element, "foo", [{"bar", "value"}]},
   {:start_document, [version: "1.0", encoding: "UTF-8", standalone: false]}
 ]}
Link to this function parse_string(data, handler, state) View Source
parse_string(
  data :: binary(),
  handler :: module() | function(),
  state :: term()
) :: {:ok, state :: term()} | {:error, exception :: Saxy.ParsingError.t()}

Parses XML binary data.

This function takes XML binary, SAX event handler (see more at Saxy.Handler) and an initial state as the input, it returns {:ok, state} if parsing is successful, otherwise {:error, exception}, where exception is a Saxy.ParsingError struct which can be converted into readable message with Exception.message/1.

The third argument state can be used to keep track of data and parsing progress when parsing is happening, which will be returned when parsing finishes.

Examples

defmodule MyEventHandler do
  @behaviour Saxy.Handler

  def handle_event(:start_document, prolog, state) do
    IO.inspect "Start parsing document"
    [{:start_document, prolog} | state]
  end

  def handle_event(:end_document, _data, state) do
    IO.inspect "Finish parsing document"
    [{:end_document} | state]
  end

  def handle_event(:start_element, {name, attributes}, state) do
    IO.inspect "Start parsing element #{name} with attributes #{inspect(attributes)}"
    [{:start_element, name, attributes} | state]
  end

  def handle_event(:end_element, {name}, state) do
    IO.inspect "Finish parsing element #{name}"
    [{:end_element, name} | state]
  end

  def handle_event(:characters, chars, state) do
    IO.inspect "Receive characters #{chars}"
    [{:chacters, chars} | state]
  end
end

iex> xml = "<?xml version='1.0' ?><foo bar='value'></foo>"
iex> Saxy.parse_string(xml, MyEventHandler, [])
{:ok,
 [
   {:end_document},
   {:end_element, "foo"},
   {:start_element, "foo", [{"bar", "value"}]},
   {:start_document, [version: "1.0", encoding: "UTF-8", standalone: false]}
 ]}