LuaXML'XMLDecl不在开始文档[char = 1]'?

时间:2017-03-17 21:19:43

标签: xml lua xml-parsing luaxml

我正在使用LuaXML来解析XML文件。我正在使用LuaXML存储库中的示例来测试库,现在我只是将我的文件放在解析器中,如下所示:

local filename = "dir/myxml.xml"
local xmltext = ""
local f, e = io.open(filename, "r")

if f then
    --Gets the entire file content and stores into a string
    xmltext = f:read("*a")
else
    error(e)
end

--Instantiate the object the states the XML file as a Lua table
local xmlhandler = simpleTreeHandler()

--Instantiate the object that parses the XML to a Lua table
local xmlparser = xmlParser(xmlhandler)
xmlparser:parse(xmltext)

对于示例文件和我在网上找到的另一个例子,代码完美无缺。但是,对于我要解析的文件,我收到以下错误:

...dir/LuaXML/xml.lua:170: XMLDecl not at start of document [char=1]

我的解释是XML声明不正确,但XML文件的第一行是:

 <?xml version="1.0" encoding="utf-8"?>

与示例相同/相似。此外,第一行之前没有空格。

谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明,这个错误是由于BOM造成的。为了纠正它,我在Notepad ++中打开了我的XML文件,然后执行了以下操作

编码 - &gt;用UTF-8编码。

当我重新编写程序时,我没有错误。