问题描述
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">>,From_Jid} = fxml:get_tag_attr(<<"from">>,Type} = fxml:get_tag_attr(<<"type">>,
我能够成功地从 XML 中读取数据,如上所示。在某些情况下,数据包 中不存在 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 中?
解决方法
也许您应该使用 xmpp 库,而不是较低级别的 fxml。例如,var input_time = "10:46";
var date_a = moment(input_time,"HH:mm");
console.log(date_a);
>> Moment {...} // moment object
var date_a_str = date_a.format("HH:mm");
console.log(date_a_str);
>> "10:46" // string
,请参阅:
https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1
您应该为此使用 case
:
case fxml:get_tag_attr(<<"type">>,EncodedData) of
false -> ...;
{_,Type} ->...
end