ejabberd - Comments for "mysql result to xml then to iq " https://www.ejabberd.im/node/4616 en I wrote this small module. https://www.ejabberd.im/node/4616#comment-57201 <p>I wrote this small module. Copy the functions you want from here:</p> <pre> -module(testm). -export([test/0]). -include("ejabberd.hrl"). -include("jlib.hrl"). -define(NS_NAMES, "test-names"). %%% %%% Convert a Mysql result into IQ result %%% make_iq(MysqlResult) -&gt; ResultXmls = make_results(MysqlResult), #iq{type = result, sub_el = [{xmlelement, "result", [{"xmlns", ?NS_NAMES}], [{xmlelement, "userinfo", [], ResultXmls}]}]}. make_results({selected, ["id", "first_name", "last_name"], Items}) -&gt; [make_result(Item) || Item &lt;- Items]. make_result({Id, First, Last}) -&gt; SubEls = [ {xmlelement, "first_name", [], [{xmlcdata, First}]}, {xmlelement, "last_name", [], [{xmlcdata, Last}]} ], {xmlelement, "result", [{"id", integer_to_list(Id)}], SubEls}. %%% %%% Test code %%% test() -&gt; MysqlResult = test_input(), IQXml = make_iq(MysqlResult), IQString = xml:element_to_string(jlib:iq_to_xml(IQXml)), ?INFO_MSG("IQ built: ~n~s~n~nDesired result:~n~s", [IQString, test_wanted()]), test_finished. test_input() -&gt; {selected,["id","first_name","last_name"], [{1,"Matt","Williamson"}, {2,"Matt","Williamson2"}]}. test_wanted() -&gt; "&lt;result id='1'&gt; &lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson&lt;last_name&gt; &lt;result&gt; &lt;result id='2'&gt; &lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson2&lt;last_name&gt; &lt;result&gt;". </pre><p> This is the result I get when I run the test/0 function:</p> <pre> (ejabberd@localhost)1&gt; testm:test(). test_finished =INFO REPORT==== 30-Mar-2011::19:08:11 === I(&lt;0.43.0&gt;:testm:37) : IQ built: &lt;iq type='result'&gt;&lt;result xmlns='test-names'&gt;&lt;userinfo&gt;&lt;result id='1'&gt;&lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson&lt;last_name&gt;&lt;result&gt;&lt;result id='2'&gt;&lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson2&lt;last_name&gt;&lt;result&gt;&lt;userinfo&gt;&lt;result&gt;&lt;iq&gt; Desired result: &lt;result id='1'&gt; &lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson&lt;last_name&gt; &lt;result&gt; &lt;result id='2'&gt; &lt;first_name&gt;Matt&lt;first_name&gt; &lt;last_name&gt;Williamson2&lt;last_name&gt; &lt;result&gt; </pre> Wed, 30 Mar 2011 17:13:01 +0000 mfoss comment 57201 at https://www.ejabberd.im