• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

manoelcampos/xml2lua: XML Parser written entirely in Lua that works for Lua 5.1+ ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

manoelcampos/xml2lua

开源软件地址(OpenSource Url):

https://github.com/manoelcampos/xml2lua

开源编程语言(OpenSource Language):

Lua 99.1%

开源软件介绍(OpenSource Introduction):

xml2lua Build Status MIT license LuaRocks

Buy Me A Coffee

conversion ways

xml2lua is an XML parser written entirely in Lua which doesn’t depend on any external C/C++ library. It works for Lua 5.1 to 5.3 and enables:

  • parsing an XML string into a Lua Table;

  • converting a Lua Table to an XML string.

This version was adapted to work with Lua 5 and can be used in Lua applications, including interactive Digital Television (DTV) Ginga NCL applications for the Brazilian DTV System (worldwide known as ISDB-T International or ISDB-Tb).

The original parser was written by Paul Chakravarti and is available on LuaUsers.

1. Installation

1.1. From LuaRocks repository

The best way to download the module is using LuaRocks at the command line:

luarocks install xml2lua

1.2. From local sources

Download the source files using the download button above or type at a terminal:

git clone https://github.com/manoelcampos/xml2lua.git

Finally, enter the directory where the source files were downloaded and type:

luarocks make

2. How to use

2.1. Parsing an XML String into a Lua Table

A simplified example which parses an XML directly from a string is presented below. There are some caveats to deal with when an XML has just one tag. Check the example1.lua for details.

local xml2lua = require("xml2lua")
--Uses a handler that converts the XML to a Lua table
local handler = require("xmlhandler.tree")

local 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>
]]

--Instantiates the XML parser
local parser = xml2lua.parser(handler)
parser:parse(xml)

--Manually prints the table (since the XML structure for this example is previously known)
for i, p in pairs(handler.root.people.person) do
  print(i, "Name:", p.name, "City:", p.city, "Type:", p._attr.type)
end

2.2. Converting a Lua Table to an XML String

local xml2lua = require("xml2lua")
local people = {
    person = {
        {name="Manoel", city="Palmas-TO", _attr={ type='natural' } },
        {name="Breno", city="Palmas-TO", _attr={ type='legal' } }
    }
}

print("People Table\n")
xml2lua.printable(people)

print()
print("XML Representation\n")
print(xml2lua.toXml(people, "people"))

3. Command line tool

You can use a command line tool to try parsing XML files. Execute lua testxml.lua -help on the terminal for more details.

4. Running tests

4.1. Requeriments

4.2. How to

make lint # to run the lint check
make test # to run the acceptance tests

5. License

This code is freely distributable under the terms of the MIT license.

6. Authors




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
JaapBraam/LoRaWanGateway: A LoRaWan Gateway in LUA发布时间:2022-08-16
下一篇:
756915370/LuaRuntimeHotfix: Lua runtime update module in unity发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap