mightwork
mightwork

Reputation: 53

Parse packet xmel in Ejabberd 13.12

I am capturing packets using the filter_packet hook. Now how can I parse this xmlel and get the value of the desired tags ?

Upvotes: 1

Views: 998

Answers (2)

Odobenus Rosmarus
Odobenus Rosmarus

Reputation: 5998

actually when you use hooks in the ejabberd or in the mongooseim, you'll get already parsed message. And you can retrieve 'body' subelement from that parsed structure using xml or exml libraries. For instance:

 exml_query:subelement(YourMessage, <<"body">>),

or

 xml:get_subtag(Message, <<"body">>),

or you can iterate manually all subelements and do something with them

   #xmlel{name= <<"message">>, children= Els} = Message,
   [ do_something(Body) || #xmlel{name = <<"body">>} = Body <- Els ]

When you retrieve body element, you can extract its value

xml:get_tag_cdata(Body)

or

exml_query:cdata(Body)

As an option, if you are sure that message contains body, you can extract value of body at once

exml_query:path(Message, [{element, <<"body">>},cdata])

Upvotes: 2

Dmitry Belyaev
Dmitry Belyaev

Reputation: 2593

I'm almost sure there must be already parsed xmlel object passed which is quite easy to deconstruct. Anyway, if the only thing you managed to get from ejabberd is just raw stanza you can use xmerl to do the job. It is documented and has user's guide to start with.

1> S = <<"<message ...">>.
2> (fun(Str) -> {Root, []} = xmerl_scan:string(Str), [{xmlText, _, _, _, Text, _}] = xmerl_xpath:string("//body/text()", Root), Text end)(binary_to_list(S)).
"as"
3> 

Upvotes: 1

Related Questions