XML "Library" and File Functions

XML Library

File Functions

I expect this could be help some people ^^

– Edit

-- Exemple of xml library for a Class

Object = class()

function Object:init()
     self.x = 10
     self.tst = "Texte"
end

function Object:toXml()
    return tostring(XML(self, "Object"))
end

function Object.fromXml(xml)
    xml = XML(xml).racine["Object"]
    local obj = Object()
    for k,v in pairs(xml) do
        -- table or object gestion 
        if type(obj[k]) == "number" then
            obj[k] = tonumber(v)
        else
            obj[k] = v
        end
    end
    
    return obj
end

-- Main

function setup()
    local obj = Object()
    local xml = obj:toXml()
    local obj2 = Object.fromXml(xml)
end


```

Thanks for sharing

@HydroVitalyProtago looks great!
Could you use the ‘simple’ code format for your example above? I cant copy it from the forum in this format. Thanks.
[edit] never mind, now it is ok. Safari + this forum act weird when swing between apps…

I tried both: really cool! Works great.

@Jmv38 Thanks for feedback !

Thank YOU for sharing.
The stuff is so good i think i’ll use it for game data saving from now on, rather that saveglobal/local/project/data, which is always confusing. Having everything in one single clear text file is much simpler, as long as the in/out tools are simple to use - and they are.

@Jmv38 Thanks ! This is what a use for my current project AllFighters because i want to make it in lua and in java.

In some time, i add a new physics function :

 physics.make(image) 

who return the physics body of an image.

And a little plus


-- string.replace in all tab of your project
function project_replaceAll(pattern, replacement)
    local tabs = listProjectTabs()
    for k,v in pairs(tabs) do
        local file = readProjectTab(v)
        file = string.replace(file, pattern, replacement)
        saveProjectTab(v, file)
    end
end

The replaceAll is really a missing function in codea…
However i would hesitate to use your code because of ‘pattern’ which can be dangerous. Maybe instead of ‘pattern’ it could be ‘text’ and inside the function the pattern is defined to replace only the isolated ‘text’ occurence? To avoid inside-a-word unwanted replace? But it should still work with text(), text:xxx, xxx:text.yyy , text+2, etc… Tricky.

It all depends of your pattern, you can use “%stext%s” if want just replace the word ‘text’.

Exactly!
Since i am not an expert with patterns, i’ll be careful. That’s all i am saying.
Also, the pattern you show above will work for isolated words, but not all the special cases i have mentionnioned. We have a couple pattern black belt on this forum, maybe they will suggest something?

inside a word (word contains only letter) : “%s%atext%a%s”.

Also, for the rest, we need more information about what xxx can be (letters ? numbers ? punctuation ? all ?)

I think that know more about pattern is important. This link is great : http://www.lua.org/pil/20.2.html

For people who want directly this library, this is the installer

@HyroVitalyProtage When you use the version control part of AutoGist make sure to comment out and AutoGist class code with:

--/*

--*/

So people without the library can run it without errors :slight_smile:

@Briarfox Thanks !