-- 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
```
@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…
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.
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?