An XML DOM Level 2 Core implementation in Lua, based on the (Lua)Expat parser.
local DOM = require("expadom.DOMImplementation")()
local doc = DOM:createDocument(nil, "root")
local root = doc.documentElement
root:appendChild(doc:createComment("let's create an address list"))
local list = doc:createElement("addresses")
list:setAttribute("country", "Netherlands")
root:appendChild(list)
local addr = doc:createElement("address")
list:appendChild(addr)
addr:appendChild(doc:createTextNode("address goes here"))
local xml_written = table.concat(doc:write())
-- result (formatting added for readability):
-- <?xml version="1.0" encoding="UTF-8"?>
-- <root>
-- <!--let's create an address list-->
-- <addresses country="Netherlands">
-- <address>address goes here</address>
-- </addresses>
-- </root>
-- now parse the document again:
local xml_parsed = require("expadom").parseDocument(xml_written)
local address = xml_parsed:getElementsByTagName("address")[1]
print(address.childNodes[1].nodeValue) --> "address goes here"
The documentation and reference is available in the /docs
folder, and online.
Source code and downloads are available from the Github project page. Installation is typically easiest using LuaRocks.
Expadom depends on the following packages:
- LuaExpat for parsing XML. This requires that Expat itself is also installed.
- The Lua module compat53 is required
for UTF-8 support on Lua versions lacking the
utf8
module (pre Lua 5.3).
When installing through LuaRocks, Expat
must be installed manually, the other
dependencies will be dealt with by LuaRocks.
The project is licensed under the MIT License
none...