ERLANG将字符串转换为元组列表

假蜜蜂

我有一个用户名字符串,形式为“ hello1 @ devlab,hello2 @ devlab,hello3 @ devlab”。此字符串可以有N个用户名。

我想将其转换为以下形式的数据包:

Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"ABCMSG">>}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello1@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello2@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello3@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]}
]
}]},

因此,通过添加额外的xmlel元组,该数据包应该能够容纳N个用户名(根据字符串中的用户名数量):-

Packet_in_tuple_form={xmlel,<<"message">>,[{<<"id">>,<<"rkX6Q-8">>},{<<"to">>,<<"multicast.devlab">>}],[{xmlel,<<"body">>,[],[{xmlcdata,<<"ABCMSG">>}]},{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello1@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello2@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello3@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello4@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"hello5@devlab">>},{<<"desc">>,<<"description goes here!">>}],[]}
]
}]},

我怎样才能做到这一点?

感谢和问候

丹尼尔·奥尼琴科(Danil Onishchenko)

这是一个如何将字符串转换为地址列表的示例:

Str = "hello1@devlab, hello2@devlab, hello3@devlab",
Addrs = [I || I <- string:tokens(Str, ", ")].

因此,可以通过以下方式为您的数据包生成地址列表Addrs

[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,list_to_binary(JID)},{<<"desc">>,<<"description goes here!">>}],[]} || JID <- Addrs].

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章