Created
August 29, 2013 06:07
-
-
Save whoo24/6374697 to your computer and use it in GitHub Desktop.
LuaExpat sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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