在Lua中安全地生成XML [英] Safely generating XML in Lua

查看:206
本文介绍了在Lua中安全地生成XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个生成Atom提要的Lua应用程序.现在,我正在手工"生成XML-将字符串写入文件.

I'm writing a Lua application that generates an Atom feed. Right now, I'm generating XML "by hand" -- writing strings to files.

虽然可能是最佳方法,但这似乎并不是最好的方法.我对于完全正确的转义感到不安.以前有人在Lua中做过这样的事情吗?

This doesn't seem like the best way, although it may be. I am getting nervous about having the escaping exactly right. Has anyone done anything like this in Lua before?

我应该坚持使用手工"生成吗?还是为现有的C库编写包装器?

Should I stick with generating "by hand"? Or write a wrapper for an existing C library?

(相比之下,Perl似乎具有

(Perl, by comparison, seems to have a plethora of options.)

推荐答案

我现在对我的问题还有一个答案:我已经为

I've now got one more answer to my question: I've written Lua bindings for Tim Bray's C XML generator, Genx.

这是它的首页;这是在Bitbucket上的项目.现在这是一个不完整的绑定,但是它适用于简单的任务:

Here's its homepage; here's the project on Bitbucket. It's an incomplete binding right now, but it works for simple tasks:

require 'genx'
d = genx.new(io.stdout)

d:startElement('farm')

d:startElement('crop')
d:attribute('type', 'fruit')
d:text('Apricots')
d:endElement()

d:startElement('crop')
d:attribute('type', 'vegetable')
d:text('Zucchini')
d:endElement()

d:endElement()

产生:

<farm><crop type="fruit">Apricots</crop><crop type="vegetable">Zucchini</crop></farm>

就个人而言,我发现这种流"界面比基于树的界面更易于使用.

Personally, I find this kind of "streaming" interface slightly nicer to work with than a tree-based interface for generation.

这篇关于在Lua中安全地生成XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆