Last active
May 8, 2022 23:29
-
-
Save JoeZ99/eab68cb5d90fc8a99023e252ab6ebcc9 to your computer and use it in GitHub Desktop.
Use sax to make stream output out of parsing xml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule XmlStreamer do | |
defp to_elements_stream(xml_stream) do | |
{:ok, partial} = Saxy.Partial.new(XmlEventHandler) | |
Stream.chunk_while(xml_stream, partial, &maybe_items/2, &finalize/1) | |
end | |
defp maybe_items(chunk, partial) do | |
with {:cont, partial} <- Saxy.Partial.parse(partial, chunk), | |
{:ok, partial, items} <- fetch_items(partial) do | |
if items == [], do: {:cont, partial}, else: {:cont, items, partial} | |
else | |
{:error, exception} when is_exception(exception) -> | |
raise Saxy.ParseError.message(exception) | |
{:error, reason} -> | |
raise reason | |
_ -> | |
raise "Xml parsing error" | |
end | |
end | |
defp finalize(partial) do | |
case Saxy.Partial.terminate(partial) do | |
{:ok, %{items: []}} -> | |
{:cont, %{}} | |
{:ok, %{items: items}} -> | |
{:cont, items, %{}} | |
{:error, exception} when is_exception(exception) -> | |
raise Saxy.ParseError.message(exception) | |
end | |
end | |
defp fetch_items(%{state: %{user_state: %{items: items} = user_state} = state} = partial) do | |
partial = %{partial | state: %{state | user_state: %{user_state | items: []}}} | |
{:ok, partial, items} | |
end | |
defp fetch_items(_), do: {:error, "Something wrong when processing sax events"} | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to be used with https://gist.github.com/JoeZ99/fe979b9e781e5633e8917f737a25671a