erlang mysql结果到xml

发布于 2024-10-27 22:04:47 字数 760 浏览 2 评论 0原文

我有一个输出:

MysqlResult = {selected,["id","first_name","last_name"],
         [{1,"Matt","Williamson"},
         {2,"Matt","Williamson2"}]}

如何使其看起来像:

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>"

我正在寻找一种将其放入 IQ 中的智能方法(ejabberd)

IQ#iq{type = result, sub_el =
                   [{xmlelement, "result",
                   [{"xmlns", ?NS_NAMES}],
                   [{xmlelement, "userinfo", [],
                   [{xmlcdata,"???"??  }]}]}]}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℡寂寞咖啡 2024-11-03 22:04:47

首先从元组中提取结果元素:

{selected, _Columns, Results} = MysqlResult.

然后使用列表理解将其转换为 ejabberd 的内部 XML 格式:

XML = [{xmlelement, "result", [{"id", integer_to_list(Id)}],
        [{xmlelement, "first_name", [], [{xmlcdata, FirstName}]},
         {xmlelement, "last_name", [], [{xmlcdata, LastName}]}]}
       || {Id, FirstName, LastName} <- Results].

并将其插入到您的 IQ 记录中:(

IQ#iq{type = result, sub_el =
                   [{xmlelement, "result",
                   [{"xmlns", ?NS_NAMES}],
                   [{xmlelement, "userinfo", [],
                   XML}]}]}

假设您想要 元素作为 元素的子元素)

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)

噩梦成真你也成魔 2024-11-03 22:04:47

使用 xmerl 在 Erlang 中创建 XML:

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

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
呆橘 2024-11-03 22:04:47

尝试在 mysql 命令行客户端中使用 --xml 和 --execute 选项。

mysql客户端

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

mysql client

☆獨立☆ 2024-11-03 22:04:47

xmerl 解决方案绝对没问题,如果这是一次性的事情,可能是可行的方法。

但是,如果您正在编写 xmpp 客户端,即使是一个简单的客户端,也可以考虑使用 exmpp - https://github.com/ processone/exmpp 。您可以使用一些策略来提取数据并生成 XML,但一般来说,辅助函数(最有可能在 exmpp_iq 和 exmpp_stanza 模块内)将非常方便。

exmpp 也不会去任何地方——ejabberd3 的 alpha 正在内部使用它(最终)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文