manoelcampos / xml2lua

XML Parser written entirely in Lua that works for Lua 5.1+. Convert XML to and from Lua Tables 🌖💱
MIT License
287 stars 73 forks source link

xml2lua crashes on multiple same empty elements #23

Closed blueowl04 closed 5 years ago

blueowl04 commented 5 years ago

I have found that if a XML file contains two identical elements that are empty, xml2lua can't cope with that and crashes. This is a sample of problematic XML:

<item>
    <values>
        <value>Lorem</value>
        <value>ipsum</value>
        <value>dolor</value>
        <value></value>
        <value></value>
    </values>
</item>

It produces the following error:

lua: /usr/share/lua/5.3/xmlhandler/tree.lua:118: bad argument #1 to 'insert' (table expected, got string)
stack traceback:
    [C]: in function 'table.insert'
    /usr/share/lua/5.3/xmlhandler/tree.lua:118: in function 'xmlhandler.tree.starttag'
    /usr/share/lua/5.3/XmlParser.lua:345: in upvalue 'parseNormalTag'
    /usr/share/lua/5.3/XmlParser.lua:377: in upvalue 'parseTagType'
    /usr/share/lua/5.3/XmlParser.lua:449: in function 'XmlParser.parse'
    xml2lua-test.lua:19: in main chunk
    [C]: in ?
manoelcampos commented 5 years ago

Thanks for reporting. I've just fixed the issue.

blueowl04 commented 5 years ago

Now it works for me. Thanks!

Just a question, I have noticed that empty elements are parsed into empty tables, not strings. Is that intentional? So, for example:

<item>
  <abc>Hello</abc>
  <empty />
</item>

is translated to

{
  item = {
    empty = {},
    abc = "Hello"
  }
}
manoelcampos commented 5 years ago

Yes, it is. This is the only way to support multiple tags with the same name. Converting empty tags to string was the issue.