1
local xml2lua = require('xml2lua')
local handler = require('xmlhandler.dom')

local function count_root_nodes_in_dom(node)
    if type(node) ~= 'table' then -- trust me, I've seen functions spat at me already.
        return 0
    end

    local count = 0
    if node._type == 'ROOT' then
        count = count + 1
    end
    for _, child in ipairs(node._children) do
        count = count + count_root_nodes_in_dom(child)
    end
    return count
end

local function count_root_nodes()
    local parser = xml2lua.parser(handler)
    parser:parse('<xml></xml>')
    return count_root_nodes_in_dom(handler.root)
end

for _ = 1, 16 do
    print(count_root_nodes())
end

For immediate assistance, please email our customer support: [email protected]

Download RAW File