erlang mysql multiple row result to xml -
i have multiple row result of mysql select in erlang :
{_,_, result} = ejabberd_odbc:sql_query(server, [<<"select group, group_concat(members.username separator ', ') member members id='">>,id,<<"'">>]),
result = [{"group","username1,username2, username3 ......."}.... {"group","username1,username2, username3 ......."}]
want convert results xml element :
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>
any easy way place xmpp iq sub element in loop :
subel = [#xmlel{name = <<"members">>, attrs = [{<<"xmlns">>, ?ns_chat}], children = [#xmlel{name=<<"member">>, attrs = [{<<"username">>, username1}], children = []}] [#xmlel{name=<<"member">>, attrs = [{<<"username">>, username2}], children = []}] ............. }],
here approach might work you:
create_xml() -> % 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 folding on 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>"].
running above, get: 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>"
Comments
Post a Comment