manoelcampos / xml2lua

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

Error: Incomplete XML Document [char=xx] #4

Closed sigmasoldi3r closed 7 years ago

sigmasoldi3r commented 7 years ago

Hello there! I'm trying to implement this parser in Garrysmod (A game that uses a... uh, a "custom" implementation of Lua, wich is horrible) and the script throws this error in whatever XML string I pass to the parser:

Stacktrace

[ERROR] addons/compiler/lua/includes/modules/xml2lua.lua:268: Incomplete XML Document [char=44]

  1. error - [C]:-1
   2. errorHandler - addons/compiler/lua/includes/modules/xml2lua.lua:268
    3. _err - addons/compiler/lua/includes/modules/xml2lua.lua:457
     4. parse - addons/compiler/lua/includes/modules/xml2lua.lua:302
      5. unknown - addons/compiler/lua/autorun/test.lua:3

Code

Original code (Moonscript):

require 'xml2lua'

x = xml2lua.parser require 'xmlhandler/tree'

t = x\parse [[

<root>
  <elem>hi</elem>
</root>
]]

print t.root.elem

Tested code (Lua):

require('xml2lua')
local x = xml2lua.parser(require('xmlhandler/tree'))
local t = x:parse([[
<root>
  <elem at="hello">hi</elem>
</root>
]])
print(t.root.elem)

XML Tried

First attempt:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <elem at="hello">hi</elem>
</root>

Second (Without headers)

<root>
  <elem at="hello">hi</elem>
</root>

Third (Without attributes)

<root>
  <elem>hi</elem>
</root>

Even I've tried adding and removing blank and newline charaters of padding, without success.

manoelcampos commented 7 years ago

Hello @sigmasoldi3r

You have some issues in your code. You are using the handler module without assigning it to a variable. That variable is the one used to access the elements into your XML. And you don't need to assign the result of x:parse to a variable t because x is already your parser.

You have just to follow the provided examples. There are also some confusions in your code because your top-level XML element is called root and the table generated after parsing the XML always have an element called root. This way, in the example below, you have to call handler.root.root.elem[1] to access the first element tag (notice there is the root which is always created by xml2lua and inside it, the root contained in your XML).

require('xml2lua')
local handler = require('xmlhandler/tree')
local parser = xml2lua.parser(handler)
parser:parse([[
<root>
  <elem at="hello">hi</elem>
</root>
]])

--Prints the entire table representing the parsed XML
--xml2lua.printable( handler.root.root)

print("elem 1:", handler.root.root.elem[1])
print("elem 1 at:", handler.root.root.elem._attr.at)