将exmpp Packet打印到Raw XML字符串

时间:2015-10-12 16:15:39

标签: erlang ejabberd exmpp

如何将数据包类型打印到原始XML字符串?在示例https://github.com/processone/exmpp/blob/master/examples/echo_client.erl中, echo_packet(MySession,Packet) - > 函数采用xmlel记录类型的参数Packet。正如帖子https://stackoverflow.com/a/31020654/579689中提到的那样尝试了函数 xml:element_to_binary / 1 ,但它没有用。

当我尝试使用以下表达式打印时

io:format("Received Presence stanza:~n~p~n~n", [xml:element_to_binary({xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"type">>,<<"chat">>},{<<"to">>,<<"x">>},{<<"id">>,<<"aaf6a">>}],[{xmlcdata,<<"\n">>},{xmlel,<<"body">>,[],[{xmlcdata,<<"wewe">>}]},{xmlcdata,<<"\n">>},{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]},{xmlcdata,<<"\n">>},{xmlel,<<"time">>,[{<<"xmlns">>,<<"urn:server-time">>},{<<"stamp">>,"2015-06-23T22:48:24Z"}],[]}]})]). 

收到以下错误

=ERROR REPORT==== 12-Oct-2015::09:06:01 ===
Error in process <0.30.0> with exit value: {undef,[{xml,element_to_binary,[{xmlel,<<7 bytes>>,[{<<8 bytes>>,<<2 bytes>>},{<<4 bytes>>,<<4 bytes>>},{<<2 bytes>>,<<1 byte>>},{<<2 bytes>>,<<5 bytes>>}],[{xmlcdata,<<1 byte>>},{xmlel,<<4 bytes>>,[],[{xmlcdata,<<4 bytes>>}]},{xmlcdata,<<1 byte>>},{xmlel,<<6 bytes>>,[{<<5 bytes>>,<<37 bytes>>}],[]},{xmlcdata,<<1 byte>>},{xmlel,<<4 bytes>>,[{<<5 bytes>>,<<15 bytes>>},{<<5 bytes>>,"2015-06-23T22:48:24Z"}],[]}]}],[]},{echo_client,loop,1,[{file,"../examples/ech... 

1 个答案:

答案 0 :(得分:1)

错误{undef,[{xml,element_to_binary,[list of N args]}]}告诉您函数xml:element_to_binary/N未定义。

浏览项目的xml模块,您可以尝试element_to_string而不是element_to_binary

然而,从效率exmpp_xml:document_to_iolist/1的角度来看,你会更适合