9mm
November 23, 2018, 11:49pm
1
I am using sweet_xml
. It raises :exit
for every imaginable problem, instead of just returning {:error, _}
I am making my own function, but the problem is it still logs red text to the console:
15:42:17.001 [error] 3907- fatal: :expected_element_start_tag
Is it possible to stop that?
defp xmap_safe(xml, fields) do
try do
{:ok, xmap(xml, fields)}
catch
:exit, _ -> {:error, %{}}
end
end
9mm
November 24, 2018, 12:17am
2
side question, why does fn -> (x, y)
not work but fn -> {x, y}
does? I can’t seem to find this referenced anywhere
alco
November 24, 2018, 12:17am
3
Those logs are coming from xmerl
which is the underlying library sweet_xml
uses. They can only be disabled by passing {:quiet, true}
option to :xmerl_scan.string
that SweetXml calls. Unfortunately, there doesn’t seem to be a way to pass that option when calling SweetXml.xpath
or SweetXml.xmap
.
2 Likes
9mm
November 24, 2018, 12:18am
4
Damn… well ok
Are there any other XML parsers that use xpath?
Context? It kind of looks like a anonymous function definition - but it’s not finished.
{x,y}
looks looks a tuple
No idea what (x,y)
is supposed to be.
1 Like
9mm
November 24, 2018, 12:24am
6
Oh, well other than end
its not missing anything.
fn -> {x, y} IO.puts(x) end
I’m passing in a map… but it doesnt work when using ()
. I always thought they were totally interchangeable but it appears theyre not
9mm
November 24, 2018, 12:26am
7
Try this:
%{a: 1} |> Enum.each(fn (x, y) -> IO.puts(x) end)
%{a: 1} |> Enum.each(fn {x, y} -> IO.puts(x) end)
%{a: 1} |> Enum.each(fn ({key,_value}) -> IO.puts(key) end)
2 Likes
Simple xmerl usage demo in Elixir
xmerl_demo.ex
defmodule XmlNode do
require Record
Record.defrecord :xmlAttribute, Record.extract(:xmlAttribute, from_lib: "xmerl/include/xmerl.hrl")
Record.defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
def from_string(xml_string, options \\ [quiet: true]) do
{doc, []} =
xml_string
|> :binary.bin_to_list
|> :xmerl_scan.string(options)
This file has been truncated. show original
https://groups.google.com/forum/#!topic/elixir-lang-talk/PVJzCMelQgQ
http://erlang.org/doc/man/xmerl_scan.html
3 Likes
I use Meeseeks personally, if you can load it all into memory then it uses a Rust backend to do it all crazy-fast. If you need a streaming interface to not load it all then something else.
1 Like