LuaXML
a module that maps between Lua and XML without much ado

LuaXML provides a minimal set of functions for the processing of XML data in Lua. It offers a very simple and natural mapping between the XML data format and Lua tables, which allows one to parse XML data just using Lua's normal table access and iteration methods: Substatements and text content is represented as array data having numerical keys, attributes and tags use string keys. This representation makes sure that the structure of XML data is exactly preserved in a read/write cycle. Since version 1.7, LuaXML consists of a well-optimized portable ISO-standard C file and a small Lua file. It is published under the same liberal licensing conditions as Lua itself (see below). It has been successfully compiled and used under Linux, various flavors of MS Windows, and MacOS X.

Download

Example (Lua 5.2)

-- import the LuaXML module as xml
xml = require("LuaXml") 
-- load XML data from file "test.xml" into local table xfile 
local xfile = xml.load("test.xml") 
-- search for substatement having the tag "scene" 
local xscene = xfile:find("scene") 
-- if this substatement is found... 
if xscene ~= nil then 
  -- ...print it to screen
  print(xscene)
  -- print attribute id and first substatement
  print( xscene.id, xscene[1] )
  -- set attribute id
  xscene["id"] = "newId"
end 


-- create a new XML object and set its tag to "root"
local x = xml.new("root")
-- append a new subordinate XML object, set its tag to "child", and its content to 123
x:append("child")[1] = 123
print(x)

Documentation

LuaXML consists of the following functions/calls:

xml = require("luaXml")
imports the LuaXML module.
LuaXML consists of a lua file (LuaXml.lua) and normally a shared library (.dll/.so), although a static linking is possible as well. Both parts are imported by this call provided that they are found in Lua's package search path.

function xml.new(arg)
creates a new LuaXML object.

function xml.append(var,tag)
appends a new subordinate LuaXML object to an existing one, optionally sets tag.

function xml.load(filename)
loads xml data from a file and returns it as table

function xml.save(var,filename)
saves a Lua var as XML file.

function xml.eval(xmlstring)
converts an XML string to a Lua table

function xml.tag(var, tag)
sets or returns the tag of a LuaXML object. This method is just "syntactic sugar" (using a typical Lua term)
that allows the writing of clearer code. LuaXML stores the tag value of an XML statement at table
index 0, hence it can be simply accessed or altered by var[0] or var[xml.TAG] (the latter is just a 
symbolic name for the value 0). However, writing var:tag() for access or var:tag("newTag") for altering 
may be more self explanatory.

function xml.str(var, indent, tag)
converts any Lua var to an xml string.

function xml.find(var, tag, attributeKey,attributeValue)
recursively parses a Lua table for a substatement fitting to the provided tag and attribute

function xml.registerCode(decoded,encoded)
registers a custom code for the conversion between non-standard characters and XML character entities

LuaXML License

LuaXML is licensed under the terms of the MIT license reproduced below, the same as Lua itself. This means that LuaXML is free software and can be freely used for both academic and commercial purposes.

Copyright (C) 2007-2010 Gerald Franz, www.viremo.de

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Jump to...