LuaExpat笔记
xml
xml是一种格式化数据交换语言, 适用于在网络上不同应用会话。
http://www.xml.com/pub/a/98/10/guide0.html
exPat
一种C语言实现的 xml 文档 解析库, 面向流文件的解析工具, 适合网络接收端使用, 边接受边解析。
expat C API
http://www.xml.com/pub/a/1999/09/expat/index.html?page=1
LuaExpat
一种基于expat的库的lua封装实现的xml解析器, C语言实现。
http://matthewwild.co.uk/projects/luaexpat/
LOM -- LUA OBJECT Model ,利用luaExpat解析器 (lxp)lua接口,写的一个lua文件,提供解析函数,将XML文档转化为 lua table。
http://matthewwild.co.uk/projects/luaexpat/lom.html
与expat C API 相对应的 luaExpat开放接口见 manul 的 Parser Object章节
http://matthewwild.co.uk/projects/luaexpat/manual.html#parser
code
expat 打印tag在流中开闭的顺序 和 文本
require"lxp" local xmlStr = [[ <elem1> 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(xmlStr) -- parses the line p:parse() -- finishes the document p:close() -- closes the parser
lom 转换xml为table后, 打印各个特征
require "lxp.lom" s = [[<abc a1="A1" a2="A2">inside tag abc</abc>]] tab = lxp.lom.parse (s) io.write("tag="..tab.tag, "\n"); io.write("content="..tab[1], "\n"); local attr1 = tab.attr[1]; io.write("attr1="..attr1, "attr1 value="..tab.attr[attr1],"\n");
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。