• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Easy XML lib (5 functions)

Mock

Mock the bear (MTB)
Joined
Jul 29, 2008
Messages
619
Reaction score
106
Location
Brazil
Hello, i made this lib for some newbe users, some users dont know how take value from XML files, so for help them i developed this lib, yes i know is unless for many users but for others is verry unsefull. (bad englhis sorry :thumbup:)

Well it's verry simple to use. i wrote an verry small tutorial on file to teach some users how to use but i will show other example.

Code:
local XML = [==[
 <?xml version="1.0" encoding="UTF-8"?>
 <vars>
    <var name="Mock" id="123"/>
    <var level="27"/>
  </vars>
 <vars2>
    <var2 name="Skyen" id="321"/>
    <var2 level="12"/>
  </vars2>
  ]==]
 local v_a = xml:open(XML)
 v_a:setSess('vars','var','mocksess')
 v_a:setSess('vars2','var2','skyensess')
 ret = v_a:readSess('mocksess')
 print(ret[1].name)
 ret2 = v_a:readSess('skyensess')
 print(ret2[1].name)
It will print:
Mock
Skyen


It's verry easy to use it, so lets install it.
Go to data/lib/functions.lua
Add this line on the file:
dofile('Easy-xml-lib.lua')
Next you can download the lib here: Pastey.net - lua paste bin
or you create file Easy-xml-lib.lua and add this code:
Code:
--[[
 * Author: Mock
 * Version: 1.00.0
 * How to use:
 > local kind = 2 -- you can change to see another mode
 > local XML = [==[
 <?xml version="1.0" encoding="UTF-8"?>
 <flags>
    <flag summonable="0"/>
    <flag attackable="0"/>
    <flag hostile="1"/>
    <flag illusionable="0"/>
    <flag convinceable="0"/>
    <flag pushable="1"/>
    <flag canpushitems="1"/>
    <flag canpushcreatures="0"/>
    <flag targetdistance="1"/>
    <flag staticattack="90"/>
    <flag runonhealth="0"/>
  </flags>
 ]==]
 > local read = xml:open(XML) -- or xml:open('<xml string="..../>')
 > read:setSess('flags','flag','mysession') -- <flags>, <flag summonable="0"/>, name of this session?
 > print(type(read:getSess('mysession'))) --- Will print on console an swtring with XML code fo session flag: <flags><flag summonab...
 > if kind == 1 then
 >> table_ = read:readSess('mysession')
 > else
 >> table_ = read:readFree('flag')
 > end
 > print('-----')
 > for k,v in pairs(table_) do
 >> for m,n in pairs(v) do
 >>> print(m,n)
 >> end
 > end
 >>> print:
 table
 -----
 summonable, 0
 attackable, 0
 hostile, 1
 illusionable, 0
 convinceable, 0
 pushable, 1
 canpushitems, 1
 canpushcreatures, 0
 targetdistance, 1
 staticattack, 90
 runonhealth, 0
 
 
 * Special thanks for:
 ** Skyen hasus
 ** Magus
]]--

xml = {} -- classe
function xml:open(file,kind)
         if kind ~= nil then
            local file2 = assert(io.open(file,'r'))
            file = file2:read(-1)
            file2:close()
         end
         if file == nil then
            return error('File is empyt')
         end
         alt = string.explode(file,'\n',2)
         if alt[1] == nil or string.find(alt[1],"<?xml") == nil then
            return error('Bad xml format')
         end
         file = {text=alt[2],encoding=alt,session={}}
         setmetatable(file, {__index = xml})
         return file
end

function xml:setSess(sessname,tagname,setSess)
         xmlstr = self.text
         local f = ""
         for ret in xmlstr:gmatch('<'..sessname..'>(.-)</'..sessname..'>') do
             f = ret
         end
         if f:len() == 0 then
	        return error('Cannot find session called '..sessname..'.')
         end
         self.session[setSess] = {f,tagname=tagname,sessname=sessname}
         return TRUE
end

function xml:getSess(SessName)
   return self.session[SessName]
end

function xml:readSess(SessName)
local ret = self.session[SessName]
local t,x = {},1
for xmla in ret[1]:gmatch('<'..ret.tagname..' (.-)/>') do
        t[x] = {}
        for i, v in xmla:gmatch('([^ ]-)="(.-)"') do
            t[x][i] = v
        end
        x = x + 1
    end
    return t
end
function xml:readFree(tagname)
local ret = self.text
local t,x = {},1
for xmla in ret:gmatch('<'..tagname..' (.-)/>') do
        t[x] = {}
        for i, v in xmla:gmatch('([^ ]-)="(.-)"') do
            t[x][i] = v
        end
        x = x + 1
    end
    return t
end

Save and done,
I tested it on ScITE and otscript live debuggers and i works, and i tested on otserv(orginal) but in otserv you need string.explode funcion bade by colandus, not otserv original string.explode.
Code:
function string.trim(str)
    -- Function by Colandus
    return (str:gsub("^%s*(.-)%s*$", "%1"))
end
--19º
function string.explode(str, sep, limit)
    -- Function by Colandus
    if limit and type(limit) ~= 'number' then
        error("string.explode: limit must be a number", 2)
    end

    if #sep == 0 or #str == 0 then return end
    local pos, i, t = 1, 1, {}
    for s, e in function() return str:find(sep, pos) end do
        table.insert(t, str:sub(pos, s-1):trim())
        pos = e + 1
        i = i + 1
        if limit and i == limit then break end
    end
    table.insert(t, str:sub(pos):trim())
    return t
end

So have fun with this lib ^^
 
If thing this is very usefull, as long one know how to use it :thumbup:.
 
@Shawak
yes, but in otservers dont use XML database.
this is good for server with XML database.
 
@elf
This it's not the first lib in lua and not is the last :p
my lib is only easy to use and not for all kinds of XML tags ^^
i have to say
this lib "XMLReader.lua" its 5x better than mine XP
 
Back
Top