Lua - 我只想解析一个 XML 文件
我正在使用 LuaXml。我的项目中有 3 个文件,全部位于根级别:main.lua、test.xml 和 xml_parser.lua。
main.lua:
require("xml_parser")
local obj,err = XmlParser:ParseXmlFile("test.xml")
if(not err) then
//do something with the xml
else
print("ERROR: "..err);
end
test.xml:
<?xml version="1.0" ?>
<level>
<bg>images/bg1.png</bg>
</level>
结果: 错误:test.xml:没有这样的文件或目录
没有比这更简单的了。为什么看不到文件?
I'm Using LuaXml. I have 3 files in my project, all at the root level: main.lua, test.xml, and xml_parser.lua.
main.lua:
require("xml_parser")
local obj,err = XmlParser:ParseXmlFile("test.xml")
if(not err) then
//do something with the xml
else
print("ERROR: "..err);
end
test.xml:
<?xml version="1.0" ?>
<level>
<bg>images/bg1.png</bg>
</level>
The result:
ERROR: test.xml: No such file or directory
It couldn't get much more simple than this. Why can't it see the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许文件名是相对于当前工作目录而不是源代码目录进行解释的。我不了解 Lua,但这对于大多数语言来说都是正常的:这就是标准 unix API 的工作方式(我想 Windows 也是如此),并且大多数语言只是使用这些 API。我所知道的唯一解释相对于程序的路径的语言是 Ant。
因此,要么在运行程序之前将目录更改为项目目录,要么(更好)将相对路径替换为绝对路径。或者,如果有办法找出当前脚本的位置,您可以使用它来构造绝对路径。
Perhaps the filename is being interpreted relative to the current working directory, not the directory of the source code. I don't know Lua, but that would be normal for most languages: it's how the standard unix APIs work (i imagine Windows does the same), and most languages simply use those. The only language i know that interprets paths relative to the program is Ant.
So, either change directory into the project directory before running the program, or (better) replace the relative path with an absolute one. Or, if there's a way to find out the location of the current script, you could use that to construct an absolute path.
您可能需要使用 procmon 或 filemon 之类的工具来找出使用的文件系统 io 以及它失败的原因。
You may want to use a tool like procmon or filemon to find out what filesystem io is used, and why it fails.
Lua 不太擅长处理文件夹。我的意思是它甚至没有文件夹的概念;它将其留给通常嵌入的“主机”程序。
我查看了源代码,您遇到的错误似乎是因为 io.open 无法找到文件。发生这种情况是因为 io.open 无法推断脚本的“工作文件夹”。
如果您给它绝对路径而不是相对路径,您的程序应该可以正常工作。
如果您确实需要使用相对文件夹,我们最好的选择是使用现有的库之一,用于示例 LuaFileSystem。
Lua isn't very good at handling folders. And by that, I mean that it doesn't even have the concept of folders; it leaves that to the "host" program on which it is usually embedded.
I've looked at the source, and the error you are getting seems to happen because io.open is not able to locate the files. This happens because io.open is not able to deduce "the working folder" of your script.
Your program should work correctly if you give it the absolute path instead of the relative one.
If you really need to use relative folders, our best bet is using one of the existing libraries, for example LuaFileSystem.