我开始学习erlang,但我正在努力解决erlang中的数据类型转换问题。
我收到了来自mysql的回复:
<chat>
<message>
<field name="id">500</field>
<field name="from_user">admin@domain</field>
<field name="to_user">test@domain</field>
<field name="txt">Text message from admin to user</field>
<field name="created_at">2015-08-28 17:48:41</field>
</message>
...
<message>
<field name="id">550</field>
<field name="from_user">test@domain</field>
<field name="to_user">admin@domain</field>
<field name="txt">message from test to admin</field>
<field name="created_at">2015-08-28 17:48:42</field>
</message>
</chat>
但是我需要返回一个XML(application / xml):
Fields = [[<<"44">>,<<"admin@domain">>, <<"test@domain">>,<<"message here">>, <<"2015-09-08 10:49:26">>],
[<<"47">>,<<"test@domain">>, <<"admin@domain">>,<<"response to message">>, <<"2015-09-08 10:49:36">>]].
我已按照此问题的回复,但它无效:erlang mysql result to xml 。我没有得到这个部分:io:format(“〜s”,[v(-1)])
我尝试按照此示例进行编码:Generating XML in Erlang using xmerl
假设回复是:
-define(xml_prolog, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>").
...
make_xml(Fields) ->
Xml = xmerl:export_simple([xml_simple(Fields)],
xmerl_xml,
[{prolog, ?xml_prolog}]),
unicode:characters_to_binary(Xml).
xml_simple(Fields) ->
{chat, [{message, lists:map(
fun(Entry) ->
conversion(Entry)
end, Fields)}]}.
conversion(List_of_lists) ->
Tmp = lists:map(
fun(E) ->
entries_to_tuple_of_lists(E)
end, List_of_lists),
[ {field, [{id, Id}],
[{from_user, From}],
[{to_user, To}],
[{txt, Body}],
[{created_at, Date}]} || {Id,From,To,Body,Date} <- Tmp].
entries_to_tuple_of_lists(Entries) ->
list_to_tuple(
lists:map(
fun(Elem) ->
bitstring_to_list(Elem)
end, Entries)).
我在模块中尝试过的内容:
{{1}}
答案 0 :(得分:1)
我认为从数据示例中更改功能会更容易。结果:
-module(testxml).
-export([makeXml/1]).
-define(xml_prolog, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>").
fields_to_xml_simple2(Fields) ->
[
fun(X) -> [{K, V}] = element(X, Fields), {field, [{name, atom_to_list(K)}], [V]} end(E)
|| E <- lists:seq(2, tuple_size(Fields))].
doc_xml_simple2(Fields) ->
{chat, [{message, fields_to_xml_simple2(K)} || K <- Fields]}.
makeXml(Fields) ->
Xml = xmerl:export_simple([doc_xml_simple2(Fields)], xmerl_xml,
[{prolog, ?xml_prolog}]),
unicode:characters_to_binary(Xml).
答案 1 :(得分:0)
我不知道这是否是最佳方式,但它有效:
功能:
entries_to_tuple_lists(Entries) ->
list_to_tuple(
lists:map(
fun(Elem) ->
bitstring_to_list(Elem)
end, Entries)).
element_to_tuple_list(Fields) ->
Tmp = lists:map(
fun(E) ->
entries_to_tuple_lists(E)
end, Fields),
[{message,[{id, Id}],
[{from_user,[From]},
{to_user,[To]},
{txt,[Body]},
{created_at,[Date]}]} || {Id,From,To,Body,Date} <- Tmp].
chat_to_xml(Messages) ->
{chat, element_to_tuple_list(Messages)}.
make_xml(SqlReturn) ->
Xml = xmerl:export_simple([chat_to_xml(SqlReturn)],
xmerl_xml,
[{prolog, ?xml_prolog}]),
unicode:characters_to_binary(Xml).
获得的XML:
<chat>
<message id="500">
<from_user>admin@domain</from_user>
<to_user>test@domain</to_user>
<txt>Text message from admin to user</txt>
<created_at>2015-08-28 17:48:41</created_at>
</message>
...
<message id="550">
<from_user>test@domain</from_user>
<to_user>admin@domain</to_user>
<txt>message from test to admin</txt>
<created_at>2015-08-28 17:48:42</created_at>
</message>
</chat>