[英]How to check if XML is well-formed in Elixir
I'm receiving XML files which may not be well-formed, in which case I need to ignore them. 我收到的XML文件可能格式不正确,在这种情况下,我需要忽略它们。
I'm using SweetXml which wraps xmerl. 我正在使用包装Xmerl的SweetXml。
I have example badly formed XML which doesn't have a space between two attributes. 我有一个格式错误的XML实例,它的两个属性之间没有空格。
There is no is_well_formed function - one with a simple boolean response would be great. 没有is_well_formed函数-具有简单布尔响应的函数会很棒。
Xmerl attempts to parse the file, doesn't like it, and so sends an exit. Xmerl尝试解析文件,但不喜欢它,因此发送出口。
I haven't yet learnt about supervisors, but this looks to me like a case for them. 我还没有了解主管,但是在我看来,这对他们来说是一个案例。
Is there a rookie or simple way of handling that exit signal? 是否有新手或简单的方法来处理退出信号?
defmodule XmlIsWellFormed.WellFormed do
def is_well_formed(xml) do
import SweetXml
xml_string = to_string xml
result = xml_string |> parse # parse sends exit.
# FYI - SweetXml.parse :
# def parse(doc) do
# {parsed_doc, _} = :xmerl_scan.string(doc)
# parsed_doc
# end
# Note: inspecting result is no use because xmerl sends an exit with:
# "whitespace_required_between_attributes"
# Something like this would be handy:
# try do
# result = :xmerl_scan.string(xml)
# rescue
# :exit, _ -> nil
# end
end
end
rubbish_xml = '<rubbishml><html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US"xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml"></rubbishml>'
XmlIsWellFormed.WellFormed.is_well_formed rubbish_xml
You used try/rescue
, which only intercepts exceptions. 您使用了try/rescue
,它仅拦截异常。 Exits, on the other hand, can be intercepted with the try/catch
construct : 另一方面,可以使用try/catch
构造拦截出口:
def is_well_formed(xml) do
try do
xml |> to_string |> parse
true
catch
:exit, _ -> false
end
end
IEX will print the exit message to the console, but the program will continue to execute: IEX将退出消息打印到控制台,但是程序将继续执行:
iex> XmlIsWellFormed.WellFormed.is_well_formed ~s(<a b=""c=""/>)
3437- fatal: {whitespace_required_between_attributes}
false
iex> XmlIsWellFormed.WellFormed.is_well_formed ~s(<a b="" c=""/>)
true
However, catch
ing and rescue
ing exceptions is very uncommon in Elixir. 但是, catch
和rescue
异常在Elixir中非常罕见。 You should rather design your application with a supervision tree, so that it knows how to respawn itself properly. 您应该使用监督树来设计您的应用程序,以便它知道如何正确地重新生成自身。 Then you can just let it crash ™ , and the supervisor will take care of the rest. 然后,您可以让它崩溃 ™ ,然后主管将负责其余的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.