[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: XML (was: Re: Lua 4.0 and loadlib)
- From: "Martin Spernau" <martin@...>
- Date: Fri, 23 Nov 2001 16:24:53 +0100
Hello!
I have compiled some of this info into the lua-users wiki:
http://lua-users.org/wiki/LuaXml
if someone has objections or (better) improvements, please edit!
-Martin
----- Original Message -----
From: "Philippe Lhoste" <PhiLho@gmx.net>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Thursday, November 22, 2001 2:27 PM
Subject: XML (was: Re: Lua 4.0 and loadlib)
> Wow, thx. That clears a few points for me...
> I'd be really glad to hear your progress with the FFI thing.
Of course, but don't hold your breath :-)
> I guess in the meantime it would / could be simpler to find some c-binding
> for the special libs I might need...
Yes. Being able to call functions directly from Lua is useful and fast. For
example, for Windows programming, one can't imagine mapping all API
functions
to Lua. But using a specially made library for some often-used operations,
like GDI operations, allows to hide some ugly details, like managing
handles,
cleaning, etc.
BTW, that's the way Visual Basic (and many scripting languages) choose: a
lot of objects and methods for usual tasks, and access to the internals
(hWnd
and DLL calls) for the cases not covered by the upper layer.
> Q: anybody know of a binding to a XML-parser? (Neeed not really be expat,
> that was more a testcase for trying around with dlls)
Not that I know.
Note that Lua is quite well suited to perform this parsing itself: it has
powerful string handling functions, and excellent support of hierarchical
data.
This has been already discuted in this list.
For example (taken from the ML archive):
>>>
Subject: Re: Lua and XML
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
> Have anyone made a XML <-> Lua exporter/importer ?
Well, I wrote a quite dirty "XML importer" in Lua. It reads an XML file
and puts its structure in a table. It is very very simple, and handles
only the tag part of XML (no DTDs, no entities, no declarations, no etc.).
A string such as
<section link="hi">
<title>Hello</title>
This is only a text <ldots/> and more text
</section>
will result in a tree like
{ label = "section",
args = {link="hi"},
n = 3;
{ label = "title", n=1, args={}; "Hello"},
"This is only a text ",
{ label = "ldots", n=0, args={}},
" and more text"
}
-- Roberto
=======================================================================
stack = {n=0}
-- auxiliar function to parse tag attributes
function trataargs (s)
local arg = {}
gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
%arg[w] = a
end)
return arg
end
-- string "s" is a string with XML marks. This function parses the string
-- and puts the resulting tree in stack[1].
-- (needs Lua 3.2 [beta])
function collect (s)
local top = {n=0}
tinsert(stack, top)
local i = 1
local ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>")
while ni do
local text = strsub(s, i, ni-1)
if not strfind(text, "^%s*$") then
tinsert(top, text)
end
if empty == "/" then -- empty element tag
tinsert(top, {n=0, label=label, args=trataargs(args), empty=1})
elseif c == "" then -- start tag
top = {n=0, label=label, args=trataargs(args)}
tinsert(stack, top) -- new level
else -- end tag
local toclose = tremove(stack) -- remove top
top = stack[stack.n]
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
tinsert(top, toclose)
end
i = j+1
ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>", j)
end
local text = strsub(s, i)
if not strfind(text, "^%s*$") then
tinsert(stack[stack.n], text)
end
end
<<<
>>>
From: Eckhart =?iso-8859-1?Q?K=F6ppen?= <Eckhart.Koeppen@uni-essen.de>
Subject: Re: Lua and XML
> Have anyone made a XML <-> Lua exporter/importer ? -- I just realized
> that XML would, from a marketing perspective at least, be a great format
> to export/import objects to/from Lua !
Well, there is something I programmed which might help, the Kino XML
processor. It has wrappers for Tcl and Lua via SWIG. A Xt and an
experimental Gtk widget for displaying XML with CSS are also available.
Take a look at it at:
http://nestroy.wi-inf.uni-essen.de/Koeppen/Kino/
It is under constant development but tries to stick to the DOM, so I hope
that the interface changes remain small.
Eckhart
<<<
>>>
From: luagnome@free.fr
Subject: Re: XML
En réponse à Steve Dekorte <steve@dekorte.com>:
luagnome (http://luagnome.free.fr) includes the wrapping of libxml-1.8.x,
as it is considered as a part of gnome. It allows to parse and to generate
XML
files, with a simple api (object oriented).
Hope it helps.
>
> Anyone written an XML parser for Lua?
>
> Steve
<<<
>>>
Subject: Re: XML
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
> Anyone written an XML parser for Lua?
I have this basic skeleton that parses the "main" part of an XML string (it
does not handle meta-data like "<?" and "<!"...).
-- Roberto
function parseargs (s)
local arg = {}
gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
%arg[w] = a
end)
return arg
end
function collect (s)
local stack = {n=0}
local top = {n=0}
tinsert(stack, top)
local ni,c,label,args, empty
local i, j = 1, 1
while 1 do
ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>", j)
if not ni then break end
local text = strsub(s, i, ni-1)
if not strfind(text, "^%s*$") then
tinsert(top, text)
end
if empty == "/" then -- empty element tag
tinsert(top, {n=0, label=label, args=parseargs(args), empty=1})
elseif c == "" then -- start tag
top = {n=0, label=label, args=parseargs(args)}
tinsert(stack, top) -- new level
else -- end tag
local toclose = tremove(stack) -- remove top
top = stack[stack.n]
if stack.n < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
tinsert(top, toclose)
end
i = j+1
end
local text = strsub(s, i)
if not strfind(text, "^%s*$") then
tinsert(stack[stack.n], text)
end
if stack.n > 1 then
error("unclosed "..stack[stack.n].label)
end
return stack[1]
end
-- example
x = collect[[
<methodCall kind="xuxu">
<methodName>examples.<em>getStateName</em></methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>
]]
<<<
>>>
From: Yutaka Ueno <ueno@etl.go.jp>
Subject: Re: XML
Philippe Lhoste wrote :
>> Anyone written an XML parser for Lua?
>
>I have this file on my collection of Lua programs...
>I don't know where I found it, hope this help.
That is my test program, which is now revised.
http://www.etl.go.jp/~ueno/lua/test/
But it is only tested for a few XML files used in biology.
Probably Roberto's code provides a better skelton than mine,
but there is a difference in xml-tag descriptions with Lua tables.
XML : <methodCall kind="xuxu">
Lua by Roberto: { label="methodCall", args={kind="xuxu"} }
Lua by Ueno: { xml="methodCall", kind="xuxu" }
Because the property name "xml" never appears in XML.
This method is a bit better for terribly deep XML tags
proposed in biology.
---ueno
<<<
I hope this help. Note: the mailing list archive is available on the Lua
site: http://www.lua.org/lua-l.html
> Thx a lot for help and clarifications,
You are welcome.
> -Martin
Regards.
--
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Sent through GMX FreeMail - http://www.gmx.net