showing string from XML in newText

Hello,

I’m trying some stuff out with Corona SDK as preparation for the app I want to make, but now I’ve an error that I can’t figure out. I’m taking a string from a XML file and giving this string to variable. When I print() this variable everything’s going great. But now I want to show this string as text on the screen with the display.newText function. But when I put the name of the string variable in the space for the text string I’m getting an error. This is because corona excepts an string in the space where I’ve put the variable. How can I show a string from a variable I’ve made previously as text on the screen?

Greetings,

Can you post some code?

First I’m reading the xml file and getting the info I want with this script:

local myData = require( "mydata" ) -- custom made global variables local xml = require( "xml" ).newParser() local audioFromXML = xml:loadFile( "getAudio.xml" ) myData.audioInfoXML = {} -- for each "child" in the audioFromXML table... for i=1,#audioFromXML.child do     -- store the address in the audioInfoXML table     myData.audioInfoXML[i] = audioFromXML.child[i] end -- for each audioInfoXML, print data to terminal for i=1,#myData.audioInfoXML do     -- extract data from table and store in Local variables     -- for easier readability/access:     local songTitle = myData.audioInfoXML[i].child[1].value     local audioDLink = myData.audioInfoXML[i].child[2].value     -- print audioInfoXML data to terminal     print( "Song Title:            ", songTitle )     print("Audio Download Link:        ", audioDLink ) end  

And I’ve made a variable global (with the myData method) so I can acces it in my new scene (scene1) (I’ve done this with storyboard)

Then in scene1:

local myData = require( "mydata" ) -- Custom made global variables local widget = require( "widget" ) local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local halfW = display.contentCenterX local halfH = display.contentCenterY -- Our scene function scene:createScene( event )     local group = self.view     print( #myData.audioInfoXML )     for i=1,#myData.audioInfoXML do         local songTitle = myData.audioInfoXML[i].child[1].value         print(songTitle)     end          -- Display a background     local background = display.newImage( "assets/background.png", display.contentCenterX, display.contentCenterY, true )     group:insert( background )          local contentText = display.newText( songTitle, halfW, halfH, native.systemFont, 18 )     contentText:setFillColor( 0 )  

Then I’m getting this error:

File: /Users/ernstluring/Documents/Corona/App1/page1.lua Line: 33 Bad argument #1 to 'newText' (string expected, got nil)  

I resolved the problem! :slight_smile:

The local variable “songTitle” in scene1 needed to be a global, I didn’t think of that before. Maybe someone has a better solution than this? I’ve got a feeling it can be done better.

Edit:

Never mind, I declared the songTitle variable at the top of the page now. Now every function can access it.

Glad you got it.

Can you post some code?

First I’m reading the xml file and getting the info I want with this script:

local myData = require( "mydata" ) -- custom made global variables local xml = require( "xml" ).newParser() local audioFromXML = xml:loadFile( "getAudio.xml" ) myData.audioInfoXML = {} -- for each "child" in the audioFromXML table... for i=1,#audioFromXML.child do     -- store the address in the audioInfoXML table     myData.audioInfoXML[i] = audioFromXML.child[i] end -- for each audioInfoXML, print data to terminal for i=1,#myData.audioInfoXML do     -- extract data from table and store in Local variables     -- for easier readability/access:     local songTitle = myData.audioInfoXML[i].child[1].value     local audioDLink = myData.audioInfoXML[i].child[2].value     -- print audioInfoXML data to terminal     print( "Song Title:            ", songTitle )     print("Audio Download Link:        ", audioDLink ) end  

And I’ve made a variable global (with the myData method) so I can acces it in my new scene (scene1) (I’ve done this with storyboard)

Then in scene1:

local myData = require( "mydata" ) -- Custom made global variables local widget = require( "widget" ) local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local halfW = display.contentCenterX local halfH = display.contentCenterY -- Our scene function scene:createScene( event )     local group = self.view     print( #myData.audioInfoXML )     for i=1,#myData.audioInfoXML do         local songTitle = myData.audioInfoXML[i].child[1].value         print(songTitle)     end          -- Display a background     local background = display.newImage( "assets/background.png", display.contentCenterX, display.contentCenterY, true )     group:insert( background )          local contentText = display.newText( songTitle, halfW, halfH, native.systemFont, 18 )     contentText:setFillColor( 0 )  

Then I’m getting this error:

File: /Users/ernstluring/Documents/Corona/App1/page1.lua Line: 33 Bad argument #1 to 'newText' (string expected, got nil)  

I resolved the problem! :slight_smile:

The local variable “songTitle” in scene1 needed to be a global, I didn’t think of that before. Maybe someone has a better solution than this? I’ve got a feeling it can be done better.

Edit:

Never mind, I declared the songTitle variable at the top of the page now. Now every function can access it.

Glad you got it.