xml.ex (1155B)
- # Pleroma: A lightweight social networking server
- # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
- # Copyright © 2022 Haelwenn (lanodan) Monnier <contact+news_parse_ex@hacktivis.me>
- # SPDX-License-Identifier: AGPL-3.0-only
- defmodule NewsParseEx.XML do
- require Logger
- def string_from_xpath(_, :error), do: nil
- def string_from_xpath(xpath, doc) do
- {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
- res =
- res
- |> to_string
- |> String.trim()
- {:ok, res}
- end
- def string_from_doc(doc) do
- :xmerl_xpath.string('//text()', doc)
- |> Enum.map(fn x ->
- {:xmlObj, :string, y} = :xmerl_xpath.string('string(.)', x)
- y
- end)
- |> Enum.join()
- |> String.trim()
- end
- def parse_document(text) do
- try do
- {doc, _rest} =
- text
- |> :binary.bin_to_list()
- |> :xmerl_scan.string(quiet: true)
- {:ok, doc}
- rescue
- _e ->
- Logger.debug("Couldn't parse XML: #{inspect(text)}")
- :error
- catch
- :exit, _error ->
- Logger.debug("Couldn't parse XML: #{inspect(text)}")
- :error
- end
- end
- end