如何在Lua中解析xml文件?(How to parse xml file in Lua?)

编程入门 行业动态 更新时间:2024-10-27 00:33:57
如何在Lua中解析xml文件?(How to parse xml file in Lua?)

有一个简单的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 end

The code above, prints out the xml structure of your document.

更多推荐

本文发布于:2023-07-05 07:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   如何在   Lua   xml   parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!