在 erlang 中访问元素之前,是否有任何方法可用于检查 xml 中的元素?
Is there any method available to check element in xml before accessing it in erlang?
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 也没有 在其中输入 元素,每当我尝试访问该元素时,代码都会因
错误而崩溃
** 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 中?
你应该使用 case
:
case fxml:get_tag_attr(<<"type">>, EncodedData) of
false -> ...;
{_, Type} ->...
end
也许您应该使用 xmpp 库,而不是较低级别的 fxml。例如,xmpp:get_type/1
,参见:
https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1
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 也没有 在其中输入 元素,每当我尝试访问该元素时,代码都会因
错误而崩溃** 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 中?
你应该使用 case
:
case fxml:get_tag_attr(<<"type">>, EncodedData) of
false -> ...;
{_, Type} ->...
end
也许您应该使用 xmpp 库,而不是较低级别的 fxml。例如,xmpp:get_type/1
,参见:
https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1