Saxy.parse_string
You're seeing just the function
parse_string
, go back to Saxy module for more information.
Specs
parse_string( data :: binary(), handler :: module(), initial_state :: term(), options :: Keyword.t() ) :: {:ok, state :: term()} | {:halt, state :: term(), rest :: String.t()} | {:error, exception :: Saxy.ParseError.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.ParseError
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.
Options
See the “Shared options” section at the module documentation.
Examples
defmodule MyTestHandler do
@behaviour Saxy.Handler
def handle_event(:start_document, prolog, state) do
{:ok, [{:start_document, prolog} | state]}
end
def handle_event(:end_document, _data, state) do
{:ok, [{:end_document} | state]}
end
def handle_event(:start_element, {name, attributes}, state) do
{:ok, [{:start_element, name, attributes} | state]}
end
def handle_event(:end_element, name, state) do
{:ok, [{:end_element, name} | state]}
end
def handle_event(:characters, chars, state) do
{:ok, [{:chacters, chars} | state]}
end
end
iex> xml = "<?xml version='1.0' ?><foo bar='value'></foo>"
iex> Saxy.parse_string(xml, MyTestHandler, [])
{:ok,
[{:end_document},
{:end_element, "foo"},
{:start_element, "foo", [{"bar", "value"}]},
{:start_document, [version: "1.0"]}]}