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.toXml() is not parsing tag attributes correctly #60

Closed evandrofisico closed 3 years ago

evandrofisico commented 3 years ago

Reading a existing XML file, parsing and outputting it sometimes still results in adding <_attr> instead of adding the attributes to the tag. Example: local xml = xml2lua.loadFile(xmlfile) local parser = xml2lua.parser(handler) parser:parse(xml) print(xml2lua.toXml(handler.root))

In the input XML:

2

In the output XML:

2
      <_attr>
          <placement>static</placement>
      </_attr>

vm.xml.gz

manoelcampos commented 3 years ago

It's just a printing issue, right? Because the lua table will always have _attr fields if the XML has tag attributes.

evandrofisico commented 3 years ago

The generated table does have the _attr field, but it is moved and appears as a child node instead. So just reading a file, parsing it as a table and outputting it produces a different (and invalid) xml from the original. I believe the expected behavior would be that reading, parsing and outputting should produce the same data as the input, right? Attached are a sample program and xml file showing the behaviour. bug.tar.gz

manoelcampos commented 3 years ago

You're right. I'll take a look at it. Thanks for reporting.

manoelcampos commented 3 years ago

I used a smaller XML to make it easier to debug.

<?xml version="1.0"?>
<domain type="xen">
  <on_crash>restart</on_crash>
  <clock offset="utc" adjustment="reset"/>
  <memory unit="KiB">16777216</memory>
</domain>

Indeed, it's just an issue with the xml2lua.toXml() method.