File xml2table.lua
Internalize XML into LUA. This module allows you to transform an XML string into a Txml lua table. But first, what is Txml? Another tag based language? NO.
Im' not an XML guru, so take my personal opinions with the right doubts.
XML is a pretty flexible language, good for doing nothig, bad for doing all. Since it is really generic it may allow the reuse of of some tools... But what do you think?! Isn't grep a reused peace of code? Anyway it seems that doing XML stuff is for real men. So everithing is done in XML nowadays.
Why I don't like it much? Simple, there is no way to access it with the language. You have to parse it and transform it in a more handy format for your internal purpose, elaborate it and then retransform you data structure in XML.
So this module is for the step of internalizing an XML tree into a LUA table. This means that you will be able to travferse the tree in the same way you traverse a table.
Txml = { tag_name = "father", name = "Mario", { tag_name = "son", {"son content"} } } This table represents this xml<?xml version="1.0"?> <father name="Mario"> <son>son content</son> </father>This modules is able to convert the XML into the Txml format in a quite smart way. For example this code is valid:
father = xml2table.xml2table(the xml string you have seen before) print(father.name) print(father.son._content)and the result will be "Mario" and "son content". And now a more complex example with namespaces.
<?xml version="1.0"?> <D:multistatus xmlns:D="Dav:"> <D:response> <D:href>http://ref1</D:href> <D:propstat> <D:status>HTTP/1.1 200</D:status> </D:propstat> </D:response> <D:response> <D:href>http://ref2</D:href> <D:propstat> <D:status>HTTP/1.1 404</D:status> </D:propstat> </D:response> </D:multistatus>we can convert this to Txml in tree ways:
tml1 = xml2table.xml2table(xml2) tml2 = xml2table.xml2table(xml2,{}) tml3 = xml2table.xml2table(xml2,{["Dav:"]="d"}) The information stored is the same, but only the third is the good one. The first has "Dav:" aded to each tag name. Impossiblo to type it in LUA. The second doesn't do anithing. The third one trnasforms each shortcut to his mapped. Each shortcut to "Dav:" will be replaced with a shortcut to "d". This is nice for two reasons. First it converts the ":" to __ and so you can type it in LUA. Second you can forget what the XML uses as a shortcut, it will be replaced with "d" in any case. > print(tml1.tag_name) Dav::multistatus > print(tml1[1].tag_name) Dav::response > print(tml2.tag_name) D:multistatus > print(tml2[1].tag_name) D:response > print(tml3.tag_name) d__multistatus > print(tml3[1].tag_name) d__response > print(tml3.d__response.d__href._content) http://ref1 > xml2table.forach_son(tml3,"d__response", >> function(k) >> print(k.d__href._content) >> end) http://ref1 http://ref2
Functions
| forach_son (t, sonname, f) | This is a selective table-foreach. |
| xml2table (s, m, force_encoding) | Converts XML data in a table. |
Functions
- forach_son (t, sonname, f)
-
This is a selective table-foreach. A correct usage is forach_son(t,"D__resposnse",f).
Parameters
- t:
- sonname:
- f:
- xml2table (s, m, force_encoding)
-
Converts XML data in a table. If m is {} namespaces are not expanded If m is { ["namespace"] = "xxx" } than namespaces will be expanded according to m rules, but not listed namespaces will not be expanded If m is nil namespaces are expanded every time it is possible.
Parameters
- s: string the xml data.
- m: table the map.
- force_encoding: string To force the encoding of the XML, putting "UTF-8" solves some problems with strange encodings.
Return value:
table the resulting table or nil follwed by msg,line,col.