erlang mysql结果到xml
我有一个输出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先从元组中提取结果元素:
然后使用列表理解将其转换为 ejabberd 的内部 XML 格式:
并将其插入到您的 IQ 记录中:(
假设您想要
元素作为
元素的子元素)First extract the results element from the tuple:
Then convert it to ejabberd's internal XML format with a list comprehension:
And insert it into your IQ record:
(assuming that you want the
<result/>
elements as children of the<userinfo/>
element)使用
xmerl
在 Erlang 中创建 XML:Use
xmerl
to create XML in Erlang:尝试在 mysql 命令行客户端中使用 --xml 和 --execute 选项。
mysql客户端
Try to use --xml and --execute option in mysql command line client.
mysql client
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)