【问题标题】:ERLANG Convert String to List of TuplesERLANG 将字符串转换为元组列表
【发布时间】:2015-04-20 10:24:18
【问题描述】:

我有一串用户名,格式为 “你好1@devlab,你好2@devlab,你好3@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!">>}],[]}
]
}]},

因此,这个数据包应该能够容纳 N 个用户名 [根据字符串中用户名的数量],通过添加额外的 xmlel 元组,如下所示:-

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!">>}],[]}
]
}]},

我怎样才能做到这一点??

感谢和问候

【问题讨论】:

    标签: erlang xmpp tuples ejabberd erlang-shell


    【解决方案1】:

    以下是如何将字符串转换为地址列表的示例:

    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].
    

    【讨论】:

    • 谢谢,帮了大忙。
    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 2018-08-23
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多