我正在编写一个生成Atom提要的Lua应用程序。现在,我正在“手动”生成XML --将字符串写入文件。
这似乎不是最好的方法,尽管它可能是。我对如何正确地进行转义感到紧张。以前有人在Lua做过类似的事情吗?
我应该坚持“手工生成”吗?或者为现有的C库编写一个包装器?
(相比之下,Perl似乎有a plethora of options。)
发布于 2009-08-16 01:50:08
现在我对我的问题又有了一个答案:我已经为Tim Bray's C XML generator, Genx编写了Lua绑定。
这是它的homepage;这是project on Bitbucket。它现在是一个不完整的绑定,但它适用于简单的任务:
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>就我个人而言,我发现这种“流”接口比用于生成的基于树的接口稍微好一些。
发布于 2009-08-13 02:23:43
我也手动生成了XML。我创建了一个API,如下所示:
function XmlElement(tag, text, attr)
...
end
function XmlSubelement(root, tag, text, attr)
...
end然后使用它:
local root = XmlElement("Level0")
XmlSubelement(root, "Level1", "some text")
XmlSubelement(root, "Level1", "some more text")
local s = root:tostring()我不再有代码,但它适合在一个屏幕上包括引用。
https://stackoverflow.com/questions/1264038
复制相似问题