Skip to content

Instantly share code, notes, and snippets.

@whoo24
Created August 29, 2013 06:07
Show Gist options
  • Save whoo24/6374697 to your computer and use it in GitHub Desktop.
Save whoo24/6374697 to your computer and use it in GitHub Desktop.
LuaExpat sample
--package.cpath = package.cpath..';'..'C:\\devel\\luaexpat-1.2.0\\luaexpat-1.1.win32-lua51'
require("lxp")
sample_text = "<elem1>text<elem2>inside text</elem2>more text</elem1>"
local count = 0
callbacks = {
StartElement = function(parser, name)
io.write( string.rep(" ", count), "+", name, "\n" )
count = count + 1
end,
EndElement = function ( parser, name )
count = count - 1
io.write( string.rep(" ", count), "-", name, "\n" )
end,
CharacterData = function ( parser, string )
io.write( string.rep(" ", count), "*", string, "\n")
end
}
p = lxp.new(callbacks)
p:parse(sample_text)
p:parse() -- finishes the document
p:close()
--package.cpath = package.cpath..';'..'C:\\devel\\luaexpat-1.2.0\\luaexpat-1.1.win32-lua51'
require("lxp")
sample_text = "<elem1>text<elem2>inside text</elem2>more text</elem1>"
callbacks = {
StartElement = function(parser, name)
callbacks.CharacterData = function ( parser, string )
io.write( string, "\n")
end
end,
EndElement = function ( parser, name )
callbacks.CharacterData = false
end,
CharacterData = false
}
p = lxp.new(callbacks)
p:parse(sample_text)
p:parse()
p:close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment