0

如何将数据包类型打印为原始 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... 
4

1 回答 1

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会更适合您

于 2015-10-12T16:43:58.283 回答