Quick(lua) 中解析Xml

文件格式:

<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

<Levels>

<level>

<LevelName>level_1</LevelName>

<LevelNumber>1</LevelNumber>

<Unlock>0</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_2</LevelName>

<LevelNumber>2</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_3</LevelName>

<LevelNumber>3</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_4</LevelName>

<LevelNumber>4</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_5</LevelName>

<LevelNumber>5</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_6</LevelName>

<LevelNumber>6</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_7</LevelName>

<LevelNumber>7</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_8</LevelName>

<LevelNumber>8</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_9</LevelName>

<LevelNumber>9</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_10</LevelName>

<LevelNumber>10</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_11</LevelName>

<LevelNumber>11</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_12</LevelName>

<LevelNumber>12</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_13</LevelName>

<LevelNumber>13</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_14</LevelName>

<LevelNumber>14</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

<level>

<LevelName>level_15</LevelName>

<LevelNumber>15</LevelNumber>

<Unlock>1</Unlock>

<starNumber>0</starNumber>

<HighestScore>0</HighestScore>

</level>

</Levels>

</rss>

-----------------------------分隔线

function GameStep:Parser()

-- body
local defaulter=CCUserDefault:sharedUserDefault()
    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


    --local file='res.xmlData.Chapter1.xml'
    CCFileUtils:sharedFileUtils():addSearchPath("res/xmlData/")
  --  self:enterScene("MainScene")
    local path = CCFileUtils:sharedFileUtils():fullPathForFilename("Chapter1.xml")
    print("有路径??????????????????")
    if io.exists(path) then
        print("有路径啊")
        print(path)
    end


    local fs=io.open(path,'rb')
    local 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
            print('\n level_name:',level_name,'level_num:',level_num,'unlock:',unlock,'start_num:',start_num,'highest_score:',highest_score)
        end
    end
    fs:close()
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值