b85712d6519f9dfea3138cddc81c608de01db715
[akkoma] / lib / pleroma / web / xml / xml.ex
1 defmodule Pleroma.Web.XML do
2 require Logger
3
4 def string_from_xpath(_, :error), do: nil
5
6 def string_from_xpath(xpath, doc) do
7 {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
8
9 res =
10 res
11 |> to_string
12 |> String.trim()
13
14 if res == "", do: nil, else: res
15 end
16
17 def parse_document(text) do
18 try do
19 {doc, _rest} =
20 text
21 |> :binary.bin_to_list()
22 |> :xmerl_scan.string()
23
24 doc
25 catch
26 :exit, _error ->
27 Logger.debug("Couldn't parse XML: #{inspect(text)}")
28 :error
29 end
30 end
31 end