logo

news_parse_ex

xml.ex (1155B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # Copyright © 2022 Haelwenn (lanodan) Monnier <contact+news_parse_ex@hacktivis.me>
  4. # SPDX-License-Identifier: AGPL-3.0-only
  5. defmodule NewsParseEx.XML do
  6. require Logger
  7. def string_from_xpath(_, :error), do: nil
  8. def string_from_xpath(xpath, doc) do
  9. {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
  10. res =
  11. res
  12. |> to_string
  13. |> String.trim()
  14. {:ok, res}
  15. end
  16. def string_from_doc(doc) do
  17. :xmerl_xpath.string('//text()', doc)
  18. |> Enum.map(fn x ->
  19. {:xmlObj, :string, y} = :xmerl_xpath.string('string(.)', x)
  20. y
  21. end)
  22. |> Enum.join()
  23. |> String.trim()
  24. end
  25. def parse_document(text) do
  26. try do
  27. {doc, _rest} =
  28. text
  29. |> :binary.bin_to_list()
  30. |> :xmerl_scan.string(quiet: true)
  31. {:ok, doc}
  32. rescue
  33. _e ->
  34. Logger.debug("Couldn't parse XML: #{inspect(text)}")
  35. :error
  36. catch
  37. :exit, _error ->
  38. Logger.debug("Couldn't parse XML: #{inspect(text)}")
  39. :error
  40. end
  41. end
  42. end