Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Need help with XmlParser — Gideros Forum

Need help with XmlParser

lolyoshilolyoshi Member
edited August 2013 in Bugs and issues
Hello all,

I got the error but I don't know why and how to fix. Please help me. Thanks all.
The error is: "attempt to index local 'xml_layout_x'"
This is my code... :(
I attach my build below.

==================my code====================
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
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
=======================================================
zip
zip
Ex2.zip
528K
Ex2.zip 527.9K

Comments

Sign In or Register to comment.