gpt4 book ai didi

mysql - erlang mysql多行结果到xml

转载 作者:行者123 更新时间:2023-11-29 20:58:32 32 4
gpt4 key购买 nike

我在 erlang 中有 mysql select 的多行结果:

{_,_, Result} = ejabberd_odbc:sql_query(Server,
[<<"select group, GROUP_CONCAT(members.username separator ', ') AS member from members WHERE id='">>,Id,<<"'">>]),

结果 = [{"群组","用户名1,用户名2,用户名3 ......"}.... {"群组","用户名1,用户名2,用户名3 ......"} ]

想要将结果转换为 xml 元素:

xml = <group "xxxxxx">
<members>
<member>
<username>xxxxxx</username>
</member>
<member>
<username>xxxxxx</username>
</member>
<member>
.........
</member>
</members>
</group>
........
<group "xxxxxx">
<members>
<member>
<username>xxxxxx</username>
</member>
<member>
<username>xxxxxx</username>
</member>
<member>
.........
</member>
</members>
</group>

任何将其放入循环中的 xmpp iq 子元素的简单方法:

SubEl = [#xmlel{name = <<"members">>,
attrs = [{<<"xmlns">>, ?NS_CHAT}],
children = [#xmlel{name=<<"member">>,
attrs = [{<<"username">>, Username1}],
children = []}]
[#xmlel{name=<<"member">>,
attrs = [{<<"username">>, Username2}],
children = []}]
.............
}],

最佳答案

以下是一种可能适合您的方法:

create_xml() ->
% Your result set
Input =[{"group1","username1,username2, username3"},
{"group2","username21,username22, username23"}],

Result = lists:map(fun(Group) -> [group_header(element(1, Group)),
create_segment(Group),
"</members> </group>"]
end,
Input)
lists:flatten(Result).

% Create each group segment by folding over each group
create_segment(Group) ->
Tokenized = string:tokens(element(2, Group), ","),
lists:foldl(fun(UserName, Acc) ->
[Acc, "<member> <username>", UserName,
"</username> </member>"]
end,
"",
Tokenized).

group_header(GroupName) ->
["<group \"", GroupName, "\"", "<members>"].

运行上面的代码,我得到: t:create_xml()。

"<group \"group1\" <members> <member> <username>username1</username>  </member><member> <username>username2</username>  </member><member> <username> username3</username>  </member></members> </group><group \"group2\"<members><member> <username>username21</username>  </member><member> <username>username22</username>  </member><member> <username> username23</username>  </member></members> </group>"

关于mysql - erlang mysql多行结果到xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425968/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com