【问题标题】:Is there any method available to check element in xml before accessing it in erlang?在 erlang 中访问之前,是否有任何方法可以检查 xml 中的元素?
【发布时间】:2021-07-26 23:38:38
【问题描述】:
EncodedData = xmpp:encode(Packet),
io:format("~n EncodedData => ~p~n", [EncodedData]),
{_, Id} = fxml:get_tag_attr(<<"id">>, EncodedData),
{_, To_Jid} = fxml:get_tag_attr(<<"to">>, EncodedData),
{_, From_Jid} = fxml:get_tag_attr(<<"from">>, EncodedData),
{_, Type} = fxml:get_tag_attr(<<"type">>, EncodedData),

如上所示,我能够成功地从 XML 读取数据。在某些情况下,Packet 中不存在 type 字段,因此 EncodedData 也没有 type 元素在其中,当我尝试访问该元素时,代码会因错误而崩溃

** exception error: no match of right hand side value false
   in function  mod_http_offline:create_message/1 (/opt/fp-backend-chat/ejabberd-modules/mod_http_offline.erl, line 38)
   in call from ejabberd_hooks:safe_apply/4 (src/ejabberd_hooks.erl, line 236)
   in call from ejabberd_hooks:run_fold1/4 (src/ejabberd_hooks.erl, line 217)
   in call from ejabberd_sm:route/1 (src/ejabberd_sm.erl, line 146)
   in call from ejabberd_router:do_route/1 (src/ejabberd_router.erl, line 399)
   in call from ejabberd_router:route/1 (src/ejabberd_router.erl, line 92)
   in call from ejabberd_c2s:check_privacy_then_route/2 (src/ejabberd_c2s.erl, line 865)
   in call from xmpp_stream_in:process_authenticated_packet/2 (src/xmpp_stream_in.erl, line 714)

是否有任何方法可以找到访问元素是否存在于编码的 xml 中?

【问题讨论】:

    标签: erlang ejabberd-module


    【解决方案1】:

    也许您应该使用 xmpp 库,而不是较低级别的 fxml。例如,xmpp:get_type/1,请参阅: https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1

    【讨论】:

      【解决方案2】:

      您应该为此使用case

      case fxml:get_tag_attr(<<"type">>, EncodedData) of
        false -> ...;
        {_, Type} ->...
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        • 2014-11-15
        • 2013-09-02
        • 1970-01-01
        • 2018-11-26
        相关资源
        最近更新 更多