Is there any method available to check element in xml before accessing it in erlang?

69 views Asked by At
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),

I am successfully able to read data from the XML as shown above. In some cases the type field is not present in the Packet so the EncodedData also don't have type element in it and when ever I try to access that element the code crashed with an error of

** 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)

is there any method to find that the accessing element is present in the encoded xml or not?

2

There are 2 answers

0
Badlop On BEST ANSWER

Maybe you should use the xmpp library, not the lower-level fxml. For example, xmpp:get_type/1, see: https://github.com/processone/xmpp/blob/master/doc/API.md#get_type1

0
José M On

You should use case for that:

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