Secure XML Creation in Lua

I am writing a Lua application that generates an Atom feed. Right now I am creating XML "manually" - writing lines to files.

This does not seem to be the best way, although it may be. I'm nervous because the escape is exactly right. Has anyone done something like this in Lua before?

Should I stick to manual generation? Or write a wrapper for an existing C library?

(Perl, by comparison, has many options .)

+1
source share
3 answers

Now I have another answer to my question: I wrote Lua bindings for the XML generator Tim Bray C, Genx .

Here is the home page ; here is a project on bitbucket . This is an unfinished commitment 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() 

It produces:

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

Personally, I think that this "streaming" interface is a little nicer to work with the tree-based interface for generation.

+1
source

I also created the XML manually. I created an API that looks like this:

 function XmlElement(tag, text, attr) ... end function XmlSubelement(root, tag, text, attr) ... end 

Then, to use it:

 local root = XmlElement("Level0") XmlSubelement(root, "Level1", "some text") XmlSubelement(root, "Level1", "some more text") local s = root:tostring() 

I no longer have the code, but it works on one screen, including quoting.

+2
source

There are various data storage libraries for Lua, many of which are described in the Lua custom wiki . the page in XML contains information that might help.

Good people in the Kepler project have HTML generators that can be used to generate valid XML. (Dead link.)

There is also cosmo , which is a generic template structure that can be used to generate XML (among other things).

Edit:

I understand that caring for templates does not necessarily guarantee that the result remains well-formed. I am quite sure that Cosmo authors would be unhappy if a well-formed template ever created an incorrectly formatted document, but there is still the problem of correctly citing content. In the context where I used it, this was not a problem for me, but I also did not work with unreliable source code.

LuaForge also has projects that may be useful:

  • The Lua element tree maps an XML document to and from a table tree. It uses Expat for reading and claims to generate the correct XML when writing.

  • LuaSOAP Includes XML generation and parsing sufficient for SOAP transactions. If you decide to write your own generator, this may be a good starting point for a limited dialect such as RSS or ATOM.

In addition, I know that I saw a project that implemented a complete DOM for HTML documents and supported both parsing and document creation, but I cannot remember its name or find it today in LuaForge.

Edit 2: Here is another package that I have not noticed before, even if it is included in Lua for Windows : LuaXML . It maps between XML and the Lua table socket and can read and write XML.

+1
source

Source: https://habr.com/ru/post/1302965/


All Articles