lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> Have anyone made a XML <-> Lua exporter/importer ?

Well, I wrote a quite dirty "XML importer" in Lua. It reads an XML file
and puts its structure in a table. It is very very simple, and handles
only the tag part of XML (no DTDs, no entities, no declarations, no etc.).

A string such as

<section link="hi">
  This is only a text <ldots/> and more text

will result in a tree like

{ label = "section",
  args = {link="hi"},
  n = 3;
  { label = "title", n=1, args={}; "Hello"},
  "This is only a text ",
  { label = "ldots", n=0, args={}},
  " and more text"

-- Roberto


stack = {n=0}

-- auxiliar function to parse tag attributes
function trataargs (s)
  local arg = {}
  gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
    %arg[w] = a
  return arg

-- string "s" is a string with XML marks. This function parses the string
-- and puts the resulting tree in stack[1].
-- (needs Lua 3.2 [beta])
function collect (s)
  local top = {n=0}
  tinsert(stack, top)
  local i = 1
  local ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>")
  while ni do
    local text = strsub(s, i, ni-1)
    if not strfind(text, "^%s*$") then
      tinsert(top, text)
    if empty == "/" then  -- empty element tag
      tinsert(top, {n=0, label=label, args=trataargs(args), empty=1})
    elseif c == "" then   -- start tag
      top = {n=0, label=label, args=trataargs(args)}
      tinsert(stack, top)   -- new level
    else  -- end tag
      local toclose = tremove(stack)  -- remove top
      top = stack[stack.n]
      if toclose.label ~= label then
        error("trying to close "..toclose.label.." with "..label)
      tinsert(top, toclose)
    i = j+1
    ni,j,c,label,args, empty = strfind(s, "<(%/?)(%w+)(.-)(%/?)>", j)
  local text = strsub(s, i)
  if not strfind(text, "^%s*$") then
    tinsert(stack[stack.n], text)