[Lua] 使用 xml2lua 套件,將 XML 轉成 Lua 表格讀取資料

[Lua] 使用 xml2lua 套件,將 XML 轉成 Lua 表格讀取資料

今天需要用 Lua 來讀取一個 XML 的檔案,

查了一下,應該是有好幾個套件可以做這件事,

像 Penlight 的 pl.xml 模組,以及這次我使用的 xml2lua 模組。

沒有特別比較過好壞,

只是單純先看到 xml2lua 就先拿來用了~XD

 

1. 安裝 xml2lua

用 luarocks 就可以安裝 xml2lua,

因為它是純 Lua 寫出來的模組,因此不需要編譯其他的 C 函式庫,

就不像安裝 lua-zlib 或 lua-http 套件時,還要給額外的參數:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sudo luarocks install xml2lua
sudo luarocks install xml2lua
sudo luarocks install xml2lua

 

2. 使用 xml2lua

這邊直接拿官網的例子來跑跑看 xml2lua 吧~

先準備一個 handler 和 parser:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> xml2lua = require "xml2lua"
> handler = require("xmlhandler.tree")
> parser = xml2lua.parser(handler)
> xml2lua = require "xml2lua" > handler = require("xmlhandler.tree") > parser = xml2lua.parser(handler)
> xml2lua = require "xml2lua"
> handler = require("xmlhandler.tree")
> parser = xml2lua.parser(handler)

 

接著準備一個 XML 讓 parser 呼叫  

parser()
parser() 來解析:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> xml = [[
>> <people>
>> <person type="natural">
>> <name>Manoel</name>
>> <city>Palmas-TO</city>
>> </person>
>> <person type="legal">
>> <name>University of Brasília</name>
>> <city>Brasília-DF</city>
>> </person>
>> </people>
>> ]]
> parser:parse(xml)
> xml = [[ >> <people> >> <person type="natural"> >> <name>Manoel</name> >> <city>Palmas-TO</city> >> </person> >> <person type="legal"> >> <name>University of Brasília</name> >> <city>Brasília-DF</city> >> </person> >> </people> >> ]] > parser:parse(xml)
> xml = [[
>> <people>
>>   <person type="natural">
>>     <name>Manoel</name>
>>     <city>Palmas-TO</city>
>>   </person>
>>   <person type="legal">
>>     <name>University of Brasília</name>
>>     <city>Brasília-DF</city>
>>   </person>
>> </people>
>> ]]

> parser:parse(xml)

 

解析完的資料會放在 handler.root 這張表裡面。

如果用  

pl.pretty
pl.pretty 來看的話,會像這樣:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> pretty = require "pl.pretty"
> pretty(handler.root.people.person)
{
["arg 1"] = {
{
_attr = {
type = "natural"
},
city = "Palmas-TO",
name = "Manoel"
},
{
_attr = {
type = "legal"
},
city = "Brasília-DF",
name = "University of Brasília"
}
}
}
> pretty = require "pl.pretty" > pretty(handler.root.people.person) { ["arg 1"] = { { _attr = { type = "natural" }, city = "Palmas-TO", name = "Manoel" }, { _attr = { type = "legal" }, city = "Brasília-DF", name = "University of Brasília" } } }
> pretty = require "pl.pretty"
> pretty(handler.root.people.person)
{
  ["arg 1"] = {
    {
      _attr = {
        type = "natural"
      },
      city = "Palmas-TO",
      name = "Manoel"
    },
    {
      _attr = {
        type = "legal"
      },
      city = "Brasília-DF",
      name = "University of Brasília"
    }
  }
}

 

因為 people 下面有兩個 person,因此可以這樣取得資料:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> handler.root.people.person[1].city
Palmas-TO
> handler.root.people.person[1].name
Manoel
> handler.root.people.person[1]._attr.type
natural
> handler.root.people.person[2]._attr.type
legal
> handler.root.people.person[1].city Palmas-TO > handler.root.people.person[1].name Manoel > handler.root.people.person[1]._attr.type natural > handler.root.people.person[2]._attr.type legal
> handler.root.people.person[1].city
Palmas-TO

> handler.root.people.person[1].name
Manoel

> handler.root.people.person[1]._attr.type
natural

> handler.root.people.person[2]._attr.type
legal

 

一般在程式裡,可能會這麼取資料:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> for i, p in ipairs(handler.root.people.person) do print(string.format("%d=name(%s), city(%s), type(%s)", i, p.name, p.city, p._attr.type)) end
1=name(Manoel), city(Palmas-TO), type(natural)
2=name(University of Brasília), city(Brasília-DF), type(legal)
> for i, p in ipairs(handler.root.people.person) do print(string.format("%d=name(%s), city(%s), type(%s)", i, p.name, p.city, p._attr.type)) end 1=name(Manoel), city(Palmas-TO), type(natural) 2=name(University of Brasília), city(Brasília-DF), type(legal)
> for i, p in ipairs(handler.root.people.person) do print(string.format("%d=name(%s), city(%s), type(%s)", i, p.name, p.city, p._attr.type)) end
1=name(Manoel), city(Palmas-TO), type(natural)
2=name(University of Brasília), city(Brasília-DF), type(legal)

 

這樣就可以順利的讀取 XML 裡的資料囉~

 

參考資料:lua-users wiki: Lua Xml

(本頁面已被瀏覽過 753 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料