lua表格没有返回任何内容。

尝试从NWS网站解析XML数据。

function block_weather_feed()
nws_xml_url="http://w1.weather.gov/xml/current_obs/KLNK.xml"
nws_xml=http.request(nws_xml_url)

local nws_table={}
current_tag=''

callbacks = {
    StartElement = function (parser, name)
        print ("+ ", name," ",current_tag, "\n")
        current_tag=name
    end,
    EndElement = function (parser, name)
        print ("- ", name," ",current_tag, "\n")
    end,
    CharacterData = function (parser, string)
        print ("* ", string," ",current_tag, "\n")
        nws_table[current_tag]=string
    end
}
nws_parse=lxp.new(callbacks)
nws_parse:parse(nws_xml)
nws_parse:close()
print (nws_xml)
for key,value in pairs(nws_table) do
    print(key,"--",value)
end
end

当我循环遍历表格时,键值存在,但值却不存在。为什么… 如果我更改

nws_table[current_tag]=string

nws_table[#nws_table+1]=string

该值存在,但没有字典,这些值没有任何意义。

点赞