在用erlang访问xml中的元素之前,有没有什么方法可以检查它?

4smxwvx5  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(151)
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中读取数据,如上所示。在某些情况下,type字段不存在于Packet中,因此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中?

aurhwmvo

aurhwmvo1#

也许你应该使用xmpp库,而不是低级的fxml。https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1

pkln4tw6

pkln4tw62#

您应该使用case

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

相关问题