--这是函数测试调用
function main()
local src_filepath='d:\\Users\\Administrator\\Desktop\\Chapter1.xml'
local dest_filepath='i:\\test.xml'
local ret=copyfile(src_filepath, dest_filepath)
print('level_name level_num unlock start_num highest_score')
print('-----------------------------------------------------------')
if ret==true then
updatexml(dest_filepath,'2','0','123','345456')
end
end
--这是拷贝文件函数
function filecopy(src,dest)
if src==nil or dest==nil or src=='' or dest=='' then
return false
end
local src_fs=io.open(src,'rb')
if src_fs~=nil then
os.execute('cmd /c copy /y '..src..' '..dest)
local dest_fs=io.open(dest,'rb')
if dest_fs==nil then
return false
else
return true
end
end
end
--备注:上述实现是在Windows中通过cmd调用copy命令复制文件,在IOS中可以先读取原始文件内容,再写入新文件路径。
--如果复制成功返回true,否则返回false)
--这是更新数据的函数
function updatexml(filepath,up_level_num,up_unlock,up_start_num,up_highest_score)
local fs=io.open(filepath,'rb')
if fs~=nil then
fs_data=fs:read('*all')
local chapter_data_tb=string.split(fs_data,'</level>')
for k,v in pairs(chapter_data_tb) do
for level_name,level_num,unlock,start_num,highest_score in string.gmatch(v, '<LevelName>(.+)</LevelName>\n%s+<LevelNumber>(%d+)</LevelNumber>\n%s+<Unlock>(%d+)</Unlock>\n%s+<starNumber>(%d+)</starNumber>\n%s+<HighestScore>(%d+)</HighestScore>') do
if level_num==up_level_num then
unlock=up_unlock
start_num=up_start_num
highest_score=up_highest_score
end
print(level_name,level_num,unlock,start_num,highest_score)
end
end
end
fs:close()
end
实例:
function GameStep:XMlParseChapter(Chapternumber)
----------------下面是写的xml解析function string.split(s, delimiter)
if s==nil or s=='' or delimiter==nil then
return nil
end
local result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
CCFileUtils:sharedFileUtils():addSearchPath("res/")
local number=Chapternumber
--"Chapter1.xml"
local filename="Chapter" ..number ..".xml"
print(filename)
--local path = CCFileUtils:sharedFileUtils():fullPathForFilename("Chapter1.xml")
local path = CCFileUtils:sharedFileUtils():fullPathForFilename(filename)
print("有路径??????????????????")
if io.exists(path) then
print("有路径啊")
print(path)
end
local fs=io.open(path,'rb')
local fs_data=fs:read('*all')
print("执行了all")
local chapter_data_tb=string.split(fs_data,'</level>')
chapterdatast={}
for k,v in pairs(chapter_data_tb) do
for level_name,level_num,unlock,start_num,highest_score in string.gmatch(v, '<LevelName>(.+)</LevelName>\n%s+<LevelNumber>(%d+)</LevelNumber>\n%s+<Unlock>(%d+)</Unlock>\n%s+<starNumber>(%d+)</starNumber>\n%s+<HighestScore>(%d+)</HighestScore>') do
print('\n level_name:',level_name,'level_num:',level_num,'unlock:',unlock,'start_num:',start_num,'highest_score:',highest_score)
chapterNumberrst=0
chapterNumberrst=tonumber(level_num)
chapterdatast[chapterNumberrst]={}
chapterdatast[chapterNumberrst].level_namest=level_name
chapterdatast[chapterNumberrst].level_numst=tonumber(level_num)
chapterdatast[chapterNumberrst].unlockst=tonumber(unlock)
chapterdatast[chapterNumberrst].start_num=tonumber(start_num)
chapterdatast[chapterNumberrst].highest_score=tonumber(highest_score)
end
end
print("打印出来了 兴奋啊")
print(chapterdatast[1].unlockst)
print("打印出来了 兴奋啊")
fs:close()
end