lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


A smal example:

#!/usr/bin/env lua

require("lxp")
local stuff = {}

xmldata="<Top><A/> <B a='1'/> <B a='2'/><B a='3'/><C a='3'/></Top>"

function doFunc(parser, name, attr)
  if not (name == 'B') then return end
  stuff[#stuff+1]= attr
end

local xml = lxp.new{StartElement = doFunc}
xml:parse(xmldata)
xml:close()

print(stuff[2].a) 

==> 2

> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br 
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of 
> Michael Bauroth
> Sent: maandag 20 april 2009 14:29
> To: Lua list
> Subject: Re: Accessing "external" tables from within LuaExpat 
> callbacks
> 
> Ok, to make the question simpler ... :)
> 
> I want to fill an table (local or global ) through reading a 
> XML file with use of LuaExpat. Has eventually somebody a 
> short example to do that? Any help is highly appreciated.
> 
> Best Regards
> Micha
> 
> 
> Michael Bauroth schrieb:
> > Hi,
> > 
> > I used a simple example from LuaExpat (something like this):
> > 
> > 
> > local lom = require "lxp"
> > plugin = {}
> > 
> > function cb( tag, attribute, value )
> >   plugin[ attribute ] = value
> > end
> > 
> > local callbacks = {
> >   StartElement = function( parser, name, attributes )
> >     for _, attribute in pairs( attributes ) do
> >       cb( name, attribute, attributes[ attribute ] )
> >     end
> >   end,
> >   EndElement = function( parser, name ) end,
> >   CharacterData = false
> > }
> >     
> > local xml_file = "info.plist"
> > local parser = lxp.new( callbacks )
> > local file = assert( io.open( xml_file, "r" ) )
> >        
> > parser:parse( file:read( "*all" ) )
> > file:close();
> > collectgarbage()
> > parser:parse()
> > parser:close()
> > 
> > 
> > Everytime I try to modify a table in the referenced method 
> "cb", the 
> > application stops with an accessviolation. What I'm doing wrong?
> > 
> > Best Regards
> > Micha
> > 
> 
>