View on GitHub


An XML Parser written entirely in Lua that works for Lua 5.1 to 5.3

xml2lua Build Status MIT license

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

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.


If you clone this repository, you are downloading all the module’s sources and can run the examples directly. However, if you want to use the module inside your own project, the best way is to download it using LuaRocks at the command line:

luarocks install xml2lua

How to use

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 = [[
  <person type="natural">
  <person type="legal">
    <name>University of Brasília</name>

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

--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:",, "City:",, "Type:", p._attr.type)

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.


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