I have an output :
MysqlResult = {selected,["id","first_name","last_name"],
[{1,"Matt","Williamson"},
{2,"Matt","Williamson2"}]}
how to make it look like :
XML = "
<result id='1'>
<first_name>Matt</first_name>
<last_name>Williamson</last_name>
</result>
<result id='2'>
<first_name>Matt</first_name>
<last_name>Williamson2</last_name>
</result>"
I am looking for a smart way for placing it into IQ ( ejabberd )
IQ#iq{type = result, sub_el =
[{xmlelement, "result",
[{"xmlns", ?NS_NAMES}],
[{xmlelement, "userinfo", [],
[{xmlcdata,"???"?? }]}]}]}
Use xmerl
to create XML in Erlang:
1> MysqlResult = {selected,["id","first_name","last_name"],
1> [{1,"Matt","Williamson"},
1> {2,"Matt","Williamson2"}]}.
{selected,["id","first_name","last_name"],
[{1,"Matt","Williamson"},{2,"Matt","Williamson2"}]}
2> {selected, _Columns, Results} = MysqlResult.
{selected,["id","first_name","last_name"],
[{1,"Matt","Williamson"},{2,"Matt","Williamson2"}]}
3> Content = [{result, [{id, Id}], [{first_name, [First]}, {last_name, [Last]}]} || {Id, First, Last} <- Results].
[{result,[{id,1}],
[{first_name,["Matt"]},{last_name,["Williamson"]}]},
{result,[{id,2}],
[{first_name,["Matt"]},{last_name,["Williamson2"]}]}]
4> xmerl:export_simple(, xmerl_xml).
["<?xml version=\"1.0\"?>",
[[["<","result",[[" ","id","=\"","1","\""]],">"],
[[["<","first_name",">"],["Matt"],["</","first_name",">"]],
[["<","last_name",">"],
["Williamson"],
["</","last_name",">"]]],
["</","result",">"]],
[["<","result",[[" ","id","=\"","2","\""]],">"],
[[["<","first_name",">"],["Matt"],["</","first_name",">"]],
[["<","last_name",">"],
["Williamson2"],
["</","last_name",">"]]],
["</","result",">"]]]]
5> io:format("~s", [v(-1)]).
<?xml version="1.0"?><result id="1"><first_name>Matt</first_name><last_name>Williamson</last_name></result><result id="2"><first_name>Matt</first_name><last_name>Williamson2</last_name></result>ok
First extract the results element from the tuple:
{selected, _Columns, Results} = MysqlResult.
Then convert it to ejabberd's internal XML format with a list comprehension:
XML = [{xmlelement, "result", [{"id", integer_to_list(Id)}],
[{xmlelement, "first_name", [], [{xmlcdata, FirstName}]},
{xmlelement, "last_name", [], [{xmlcdata, LastName}]}]}
|| {Id, FirstName, LastName} <- Results].
And insert it into your IQ record:
IQ#iq{type = result, sub_el =
[{xmlelement, "result",
[{"xmlns", ?NS_NAMES}],
[{xmlelement, "userinfo", [],
XML}]}]}
(assuming that you want the <result/>
elements as children of the <userinfo/>
element)
Try to use --xml and --execute option in mysql command line client.
mysql client
The xmerl solution is absolutely fine, and probably the way to go if this is a one-off type thing.
However, if you are writing an xmpp client, even a simple one, consider using exmpp - https://github.com/processone/exmpp . You can use some of the tactics to extract data and generate XML, but in general, the helper functions (most likely within the exmpp_iq and exmpp_stanza modules) will be very handy.
exmpp isn't going anywhere either -- the alpha of ejabberd3 is using it internally (finally)