开发者

Lua - I just want to parse an XML file

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 su开发者_高级运维ch file or directory

It couldn't get much more simple than this. Why can't it see the file?


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.


You may want to use a tool like procmon or filemon to find out what filesystem io is used, and why it fails.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜