feed_parser/lib/xml.ex

26 lines
723 B
Elixir
Raw Permalink Normal View History

2019-08-31 23:02:41 +00:00
defmodule FeedParser.XML do
2019-09-01 03:25:10 +00:00
@moduledoc """
A set of helpers for working with XML. To use this module, you must `require` it.
"""
2019-08-31 23:02:41 +00:00
import Record
defrecord :xmlElement, extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
defrecord :xmlAttribute, extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
defrecord :xmlText, extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
2022-04-11 20:25:08 +00:00
@spec parse(data :: String.t()) :: {:ok, tuple()} | {:error, String.t()}
2019-08-31 23:02:41 +00:00
def parse(data) do
2022-04-12 15:42:17 +00:00
try do
{doc, _} =
2022-04-11 20:25:08 +00:00
data
|> :binary.bin_to_list()
|> :xmerl_scan.string()
2019-08-31 23:02:41 +00:00
2022-04-12 15:42:17 +00:00
{:ok, doc}
catch
:exit, reason -> {:error, "parsing XML failed: #{inspect(reason)}"}
end
2019-08-31 23:02:41 +00:00
end
end