erlang mysql result to xml

2019-07-29 22:16发布

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

4条回答
手持菜刀,她持情操
2楼-- · 2019-07-29 22:26

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)

查看更多
欢心
3楼-- · 2019-07-29 22:45

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)

查看更多
Deceive 欺骗
4楼-- · 2019-07-29 22:46

Try to use --xml and --execute option in mysql command line client.

mysql client

查看更多
叛逆
5楼-- · 2019-07-29 22:47

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
查看更多
登录 后发表回答