有一个简单的XML文件,我想用Lua解析。 我应该使用什么lua xml库? 我尝试了Lubyk的xml lib,但它对我没有任何作用。
这是我做的: $ sudo luarocks install xml
试图导入模块: require("xml")
结束于:
error loading module 'xml.core' from file '/usr/lib/lua/5.3/xml/core.so': /usr/lib/lua/5.3/xml/core.so: undefined symbol: lua_tonumber lua: /usr/share/lua/5.3/xml/init.lua:31: attempt to call a nil value (field 'Parser') stack traceback: /usr/share/lua/5.3/xml/init.lua:31: in main chunk [C]: in function 'require' lua_doc_reader.lua:3: in main chunk [C]: in ?那么,Lua中的defacto xml读取库是什么?
There is a simple xml file which I want to parse using Lua. What lua xml library should I use? I tried xml lib by Lubyk but it didn't work for me.
Here is what I did: $ sudo luarocks install xml
Tried to import the module: require("xml")
Ended up with:
error loading module 'xml.core' from file '/usr/lib/lua/5.3/xml/core.so': /usr/lib/lua/5.3/xml/core.so: undefined symbol: lua_tonumber lua: /usr/share/lua/5.3/xml/init.lua:31: attempt to call a nil value (field 'Parser') stack traceback: /usr/share/lua/5.3/xml/init.lua:31: in main chunk [C]: in function 'require' lua_doc_reader.lua:3: in main chunk [C]: in ?So, what is the defacto xml reading library in Lua?
最满意答案
好吧,在尝试使用各种xml库之后,为我工作的那个是xmlreader 。
$ sudo luarocks install lua-xmlreader
然后,根据xmlreader文档:
require('xmlreader') fhandle = io.open("temp.xml", 'r') data = fhandle:read("*a") local r = assert(xmlreader.from_string(data)) while (r:read()) do local leadingws = (' '):rep(r:depth()) if (r:node_type() == 'element') then io.write(('%s%s:'):format(leadingws, r:name())) while (r:move_to_next_attribute()) do io.write((' %s=%q'):format(r:name(), r:value())) end io.write('\n') end end上面的代码打印出文档的xml结构。
Ok, after trying to use various xml libs, the one that worked for me was xmlreader.
$ sudo luarocks install lua-xmlreader
Then, according to the xmlreader docs:
require('xmlreader') fhandle = io.open("temp.xml", 'r') data = fhandle:read("*a") local r = assert(xmlreader.from_string(data)) while (r:read()) do local leadingws = (' '):rep(r:depth()) if (r:node_type() == 'element') then io.write(('%s%s:'):format(leadingws, r:name())) while (r:move_to_next_attribute()) do io.write((' %s=%q'):format(r:name(), r:value())) end io.write('\n') end endThe code above, prints out the xml structure of your document.
更多推荐
发布评论