0

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"?>

これは、例と同じ/類似しています。また、最初の行の前に空白はありません。

ありがとう!

4

1 に答える 1