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

Can't seem to figure out xml2lua usage #44

Closed ghost closed 4 years ago

ghost commented 4 years ago

I'm attempting to parse an XML coming in via a POST call in nginx with. I created a simple lua script that ngnix will use

local xml2lua = require("xml2lua")
local handler = require("xmlhandler.tree")

local function process()
  ngx.req.read_body()
  local req_body = ngx.req.get_body_data()
  ngx.log(ngx.STDERR, "Type of body " .. type(req_body))
  ngx.log(ngx.STDERR, "the body" .. req_body)

  local thehandler = handler:new()
  local theparser = xml2lua.parser(thehandler)
  theparser:parse(req_body)
  ngx.say(xml2lua.printable(thehandler.root))
  for i, p in pairs(thehandler.root.top) do
    -- print("table size " .. table.maxn(p))
    ngx.say(i .. " Test " .. p.inner)
  end
end

return process

Then I post some simple xml `

Data MoreData SomeMoreData

`

I can print the xml body to the nginx logs but I get this error with regards to the lua table from the xml. ` 10.7.1.87, server: , request: "POST / HTTP/1.1", host: "10.4.2.231:8080" 2020/08/06 17:42:20 [] 8155#8155: *4 [lua] osphandler.lua:8: the body

Data
    <inner stuff="val2">MoreData</inner>
    <inner stuff="val3">SomeMoreData</inner>

, client: 10.7.1.87, server: , request: "POST / HTTP/1.1", host: "10.4.2.231:8080" 2020/08/06 17:42:20 [error] 8155#8155: *4 lua entry thread aborted: runtime error: /home/ec2-user/work//src/osphandler.lua:16: attempt to index field 'inner' (a nil value) stack traceback: coroutine 0: /home/ec2-user/work//src/osphandler.lua: in function </home/ec2-user/work//src/osphandler.lua:4> `

I'm new to lua so forgive any obvious issues as they aren't obvious to me.

manoelcampos commented 4 years ago

It's probably an issue with your server response, not the library. Firstly try to use the library with a local XML file.