1
votes

I'm currently working on an XML parser and I'm trying to use Lua's pattern matching tools but I'm not getting the desired result. Let's say I have this XML snippet:

<Parent>
<Child>
<Details>Text in Parent tag and Details child tag</Details>
<Division>Text in Parent tag and Division child tag</Division>
</Child>
</Parent>

I need to pull the Parent tag out into a table, followed by any child tags, and their corresponding text data. I already have the pattern for pulling the data figured out:

 DATA = "<.->(.-)<"

Likewise for pulling tags individually:

TAGS ="<(%w+)>"

However like I mentioned, I need to differentiate between tags that are nested and tags that aren't. Currently the pattern that's getting the closest result I need is:

CHILDTAG= "<%w->.-<(%w-)>"

Which should print only "Child" but it prints "Division" as well for a reason I can't comprehend. The idea behind the CHILDTAG pattern is it captures a tag IFF it had an enclosing tag, i.e , the ".-" is there to signify that it may/may not have a new line between it, however I think that's completely wrong because \n- doesn't work and that signifies a new line. I referred to the documentation and to:

https://www.fhug.org.uk/wiki/wiki/doku.php?id=plugins:understanding_lua_patterns

I use Lua 5.1. I want to parse an XML file of the following pattern. How should I go about it?

Lua XML extract from pattern

1
I think you'd better off with Lua interface to a real XML parser like expat. See lxml. - lhf

1 Answers

1
votes

Simple XML parser (Named entities in XML are not supported)

local symbols = {lt = '<', gt = '>', amp = '&', quot = '"', apos = "'", nbsp = ' ', euro = '&#8364;', copy = '&#169;', reg = '&#174;'}

local function unicode_to_utf8(codepoint)
   -- converts numeric unicode to string containing single UTF-8 character
   local t, h = {}, 127
   while codepoint > h do
      local low6 = codepoint % 64
      codepoint = (codepoint - low6) / 64
      t[#t+1] = 128 + low6
      h = 288067 % h
   end
   t[#t+1] = 254 - 2*h + codepoint
   return string.char((table.unpack or unpack)(t)):reverse()
end

local function unescape(text)
   return (
      (text..'<![CDATA[]]>'):gsub('(.-)<!%[CDATA%[(.-)]]>',
         function(not_cdata, cdata)
            return
               not_cdata
                  :gsub('%s', ' ')
                  --:gsub('  +', ' ')  -- only for html
                  :gsub('^ +', '')
                  :gsub(' +$', '')
                  :gsub('&(%w+);', symbols)
                  :gsub('&#(%d+);', function(u) return unicode_to_utf8(to_number(u)) end)
                  :gsub('&#[xX](%x+);', function(u) return unicode_to_utf8(to_number(u, 16)) end)
               ..cdata
         end
      )
   )
end

function parse_xml(xml)
   local tag_stack = {}
   local result = {find_child_by_tag = {}}
   for text_before_tag, closer, tag, attrs, self_closer in xml
      :gsub('^%s*<?xml.-?>', '')      -- remove prolog
      :gsub('^%s*<!DOCTYPE[^[>]+%[.-]>', '')
      :gsub('^%s*<!DOCTYPE.->', '')
      :gsub('<!%-%-.-%-%->', '')      -- remove comments
      :gmatch'([^<]*)<(/?)([%w_]+)(.-)(/?)>'
   do
      table.insert(result, unescape(text_before_tag))
      if result[#result] == '' then
         result[#result] = nil
      end
      if closer ~= '' then
         local parent_pos, parent
         repeat
            parent_pos = table.remove(tag_stack)
            if not parent_pos then
               error("Closing unopened tag: "..tag)
            end
            parent = result[parent_pos]
         until parent.tag == tag
         local elems = parent.elems
         for pos = parent_pos + 1, #result do
            local child = result[pos]
            table.insert(elems, child)
            if type(child) == 'table' then
               --child.find_parent = parent
               parent.find_child_by_tag[child.tag] = child
            end
            result[pos] = nil
         end
      else
         local attrs_dict = {}
         for names, value in ('\0'..attrs:gsub('%s*=%s*([\'"])(.-)%1', '\0%2\0')..'\0')
            :gsub('%z%Z*%z', function(unquoted) return unquoted:gsub('%s*=%s*([%w_]+)', '\0%1\0') end)
            :gmatch'%z(%Z*)%z(%Z*)'
         do
            local last_attr_name
            for name in names:gmatch'[%w_]+' do
               name = unescape(name)
               if last_attr_name then
                  attrs_dict[last_attr_name] = '' -- boolean attributes (such as "disabled" in html) are converted to empty strings
               end
               last_attr_name = name
            end
            if last_attr_name then
               attrs_dict[last_attr_name] = unescape(value)
            end
         end
         table.insert(result, {tag = tag, attrs = attrs_dict, elems = {}, find_child_by_tag = {}})
         if self_closer == '' then
            table.insert(tag_stack, #result)
         end
      end
   end
   for _, child in ipairs(result) do
      if type(child) == 'table' then
         result.find_child_by_tag[child.tag] = child
      end
   end
   -- Now result is a sequence of upper-level tags
   -- each tag is a table containing fields: tag (string), attrs (dictionary, may be empty), elems (array, may be empty) and find_child_by_tag (dictionary, may be empty)
   -- attrs is a dictionary of attributes
   -- elems is a sequence of elements (with preserving their order): tables (nested tags) or strings (text between <tag> and </tag>)
   return result
end

Usage example:

local xml= [[
<Parent>
<Child>
<Details>Text in Parent tag and Details child tag</Details>
<Division>Text in Parent tag and Division child tag</Division>
</Child>
</Parent>
]]
xml = parse_xml(xml)

--> both these lines print "Text in Parent tag and Division child tag"
print(xml[1].elems[1].elems[2].elems[1])
print(xml.find_child_by_tag.Parent.find_child_by_tag.Child.find_child_by_tag.Division.elems[1])

What parsed xml looks like:

xml = {
   find_child_by_tag = {Parent = ...},
   [1] = {
      tag = "Parent",
      attrs = {},
      find_child_by_tag = {Child = ...},
      elems = {
         [1] = {
            tag = "Child",
            attrs = {},
            find_child_by_tag = {Details = ..., Division = ...},
            elems = {
               [1] = {
                  tag = "Details",
                  attrs = {},
                  find_child_by_tag = {},
                  elems = {[1] = "Text in Parent tag and Details child tag"}
               },
               [2] = {
                  tag = "Division",
                  attrs = {},
                  find_child_by_tag = {},
                  elems = {[1] = "Text in Parent tag and Division child tag"}
               }
            }
         }
      }
   }
}