2013-08-25 36 views
1

我正在与Gideros合作,我得到了错误,但我不知道为什么以及如何解决。 错误是:XmlParser在Luide的Gideros中

企图指数本地 'xml_layout_x'

我的身材:http://www.mediafire.com/download/utowfooin439b66/Ex2.zip

这是我header.xml

<rss version="2.0">   
<layout> 
    <layout_x>35</layout_x> 
    <layout_y>150</layout_y> 
    <layout_width>825</layout_width> 

    <image> 
     <width>270</width> 
     <height>240</height> 
    </image> 

    <table> 
     <layout_x>25</layout_x> 

     <tablerow 
      textview_name=".Name:" 
      edittext_name_height="18" 
      edittext_name_width="245" 
      edittext_name_offset_x="150" 
      textview_born=".Born:" 
      textview_weight=".Weight:" 
      textview_bio=".Bio:" 
      edittext_bio_width="441" 
      edittext_bio_height="71" 
     />  
    </table> 

    <action> 
     <name>update</name> 
    </action> 

    <file:path bio="bio_001.txt" image="Avatar_001.png"/> 
    <file:path bio="bio_002.txt" image="Avatar_002.png"/> 

</layout> 
</rss> 

然后,这是我的代码

local function getChild(xml, name) 
    for i=1,#xml.ChildNodes do 
    if xml.ChildNodes[i].Name == name then 
    return xml.ChildNodes[i] 
    end 
    end 
    end 

    -- helper function to get all children of xml node by name 
    local function getChildren(xml, name) 
    local result = {} 
    for i=1,#xml.ChildNodes do 
    if xml.ChildNodes[i].Name == name then 
    result[#result + 1] = xml.ChildNodes[i] 
    end 
    end 
    return result 
    end 

    info = gideros.class(Sprite) 
    if info.innerSprite ~= nil then 
    info:removeChild(self.innerSprite) 
    end 
    info.innerSprite = Sprite.new() 
    info.innerSprite:setPosition(200, 10) 

    local font = TTFont.new("Roboto-Regular.ttf", 38) 
    local small_font = TTFont.new("Roboto-Regular.ttf", 30) 

    function info:init()  
    local xml = XmlParser:ParseXmlFile("header.xml") 
    local layout = getChild(xml, "layout") 
    local filepath = getChildren(layout, "file:path") 

    local logo = Bitmap.new(Texture.new("logo.png")) 
    self:addChild(logo) 
    logo:setPosition(780, 5)  

    local Update_Btn = Bitmap.new(Texture.new("Update_Btn.png")) 
    self:addChild(Update_Btn) 
    Update_Btn:setPosition(33, 23) 
    Update_Btn:addEventListener(Event.MOUSE_DOWN, 
    function() 
    -- Get source 
    local src_bio = {} 
    local src_image = {} 
    for i = 1, #filepath do 
    table.insert(src_bio, filepath[i].Attributes["bio"]) 
    table.insert(src_image, filepath[i].Attributes["image"]) 
    end 

    -- Check if file exist 
    for i = 1, #filepath do 
    local bio_file = io.open(src_bio[i]) 
    local image_file = io.open(src_image[i]) 
    if (bio_file == nil or image_file == nil) then 
    table.remove(src_bio, i) 
    table.remove(src_image, i) 
    else 
    bio_file:close() 
    image_file:close() 
    end 
    end 

    -- Get content 
    local tbInfo_List = {} 
    for i = 1, #src_bio do 
    tbInfo_List[i] = {} 
    local tbInfo_tmp1 = ReadFile(src_bio[i]) 
    local tbInfo_tmp2 = ConvertTable(tbInfo_tmp1) 
    table.insert(tbInfo_List[i], tbInfo_tmp2) 
    end 

    -- Fill content 
    local xml_layout_x = getChild(layout, "layout_x") *=> the error is HERE* 
    local layout_x = xml_layout_x.Value 
    local xml_layout_y = getChild(layout, "layout_y") 
    local layout_y = xml_layout_y.Value 
    local xml_layout_width = getChild(layout, "layout_width") 
    local layout_width = xml_layout_width.Value 

    for i = 1, #tbInfo_List do 
    local tmp_y = layout_y + layout_width*(i - 1) + 25 
    table.insert(tbInfo_List[i], src_image[i]) 
    CreateInformationTag(layout_x, tmp_y, tbInfo_List[i], layout) 
    end 
    end 
    ) 

    local Exit_Btn = Bitmap.new(Texture.new("Exit_Btn.png")) 
    self:addChild(Exit_Btn) 
    Exit_Btn:setPosition(397, 23) 
    Exit_Btn:addEventListener(Event.MOUSE_DOWN, 
    function() 
    sceneManager:changeScene("home", 1, transition, easing.outBack) 
    end 
    ) 
    end 

    local function CreateInformationTag(x,y, tbInfo, layout)  
    local image = Bitmap.new(Texture.new(tbInfo.image)) 
    local name = tbInfo.name 
    local born = tbInfo.born 
    local weight = tbInfo.weight 
    local bio = tbInfo.bio 

    --Add Image 
    image.setPosition(x, y) 
    self.innerSprite.addChild(image) 

    local xml_image = getChild(layout, "image") 
    local xml_image_width = getChild(layout, "width") 
    local image_width = xml_image_width.Value() 
    local xml_table = getChild(layout, "table") 
    local xml_table_layout_x = getChild(xml_table, "layout_x") 
    local table_layout_x = xml_table_layout_x.Value() 

    local xml_table_row = getChild(xml_table, "tablerow") 
    local tbTextView = {} 

    -- .Name 
    local xml_table_row_textview_name = getChild(xml_table_row, "textview_name") 
    table.insert(tbTextView, xml_table_row_textview_name.Value()) 
    -- .Born 
    local xml_table_row_textview_born = getChild(xml_table_row, "textview_born") 
    table.insert(tbTextView, xml_table_row_textview_born.Value()) 
    -- .Weight 
    local xml_table_row_textview_weight = getChild(xml_table_row, "textview_weight") 
    table.insert(tbTextView, xml_table_row_textview_weight.Value()) 
    -- .Bio 
    local xml_table_row_textview_bio = getChild(xml_table_row, "textview_bio") 
    table.insert(tbTextView, xml_table_row_textview_bio.Value()) 

    local table_x = x + image_width + table_layout_x 
    local table_y = y 
    local table_offset_y = 20 

    -- Add label 
    for i = 1, 4 do 
    local textfield = TextField.new(font, tbTextView[i])  
    local tmp_y = table_y + (i - 1)*table_offset_y 
    textfield:setPosition(table_x, tmp_y) 
    self.innerSprite:addChild(textfield) 
    end 

    -- Add text box 
    local edittext_name_offset_x = getChild(xml_table_row, "edittext_name_offset_x") 
    local name_offset_x = edittext_name_offset_x.Value() 
    local edittext_name_width = getChild(xml_table_row, "edittext_name_width") 
    local name_width = edittext_name_width.Value() 
    local edittext_name_height = getChild(xml_table_row, "edittext_name_height") 
    local name_height = edittext_name_height.Value() 

    -- Draw text box 
    for i = 1, 3 do 
    local line = Shape.new() 
    local tmp_y = table_y + table_offset_y*(i - 1) 
    line:setLineStyle(5, 0x000000, 1) 
    line:beginPath() 
    line:moveTo(table_x + edittext_name_x,tmp_y) 
    line:lineTo(table_x + edittext_name_x + name_width, tmp_y) 
    line:lineTo(table_x + edittext_name_x + name_width, tmp_y + name_height) 
    line:lineTo(table_x + edittext_name_x, tmp_y + name_height) 
    line:lineTo(table_x + edittext_name_x, tmp_y) 
    line:endPath() 
    self.innerSprite:addChild(line) 
    end 

    -- Draw bio text box 
    local edittext_bio_width = getChild(xml_table_row, "edittext_bio_width") 
    local bio_width = edittext_bio_width.Value() 
    local edittext_bio_height = getChild(xml_table_row, "edittext_bio_height") 
    local bio_height = edittext_bio_height.Value() 

    local line_bio = Shape.new() 
    line_bio:setLineStyle(5, 0x000000, 1) 
    line_bio:beginPath() 
    line_bio:moveTo(table_x + edittext_name_x,tmp_y) 
    line_bio:lineTo(table_x + edittext_name_x + bio_width, tmp_y) 
    line_bio:lineTo(table_x + edittext_name_x + bio_width, tmp_y + bio_height) 
    line_bio:lineTo(table_x + edittext_name_x, tmp_y + bio_height) 
    line_bio:lineTo(table_x + edittext_name_x, tmp_y) 
    line_bio:endPath() 
    self.innerSprite:addChild(line_bio) 

    -- Add text in box 
    for i = 1, 3 do 
    local textfield = TextField.new(small_font, tbInfo[i]) 
    local tmp_y = table_y + (i - 1)*table_offset_y 
    textfield:setPosition(table_x, tmp_y) 
    self.innerSprite:addChild(textfield) 
    end 

    -- Add wrap text 
    local tw = TextWrap2.new(tbInfo[4], table_x, table_y + 2*table_offset_y , 18, small_font) 
    self.innerSprite:addChild(tw) 
    end 


    function ReadFile(filename) 
    local tbInfo = {} 
    fp = io.open(filename, "r") 

    for line in fp:lines() do 
    table.insert(tbInfo, line) 
    end 
    fp:close() 
    return tbInfo 
    end 

    function ConvertTable(tbInfo) 
    local tmp = {} 
    for i = 1, #tbInfo do 
    if (i <= 4) then<br /> table.insert(tmp, tbInfo[i]) 
    else 
    local str1 = tmp[5] 
    local str2 = tbInfo[i] 
    table.remove(tmp, 5) 
    local str = str1 + str2 
    table.insert(tmp, str) 
    end 
    end 
    return tmp 
    end 
+0

我附加了我的建立对我的问题,谢谢:) – lolyoshi

+1

尝试发布一个最小的例子,显示错误的行为。 –

+0

我终于找到了错误的来源,谢谢大家 – lolyoshi

回答

0

我解决了这个问题:D xml文件在解析时会导致错误。谢谢所有