logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

xml.ex (990B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.XML do
  5. require Logger
  6. def string_from_xpath(_, :error), do: nil
  7. def string_from_xpath(xpath, doc) do
  8. try do
  9. {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
  10. res =
  11. res
  12. |> to_string
  13. |> String.trim()
  14. if res == "", do: nil, else: res
  15. catch
  16. _e ->
  17. Logger.debug("Couldn't find xpath #{xpath} in XML doc")
  18. nil
  19. end
  20. end
  21. def parse_document(text) do
  22. try do
  23. {doc, _rest} =
  24. text
  25. |> :binary.bin_to_list()
  26. |> :xmerl_scan.string(quiet: true)
  27. doc
  28. rescue
  29. _e ->
  30. Logger.debug("Couldn't parse XML: #{inspect(text)}")
  31. :error
  32. catch
  33. :exit, _error ->
  34. Logger.debug("Couldn't parse XML: #{inspect(text)}")
  35. :error
  36. end
  37. end
  38. end