XML in Corona. Not sure how to do this..

Hi, 
 
I’m doing a project where I have to read from an XML document. Unfortunately, the tutorial I read about XML in Corona didn’t work as the XML file I was given is written differently. 
 
This is the XML file…

\<?xml version="1.0" encoding="UTF-8"?\> -\<appliances\> \<appliance wattage="900" name="Coffee maker"/\> \<appliance wattage="300" name="Clothes washer"/\> \<appliance wattage="1800" name="Clothes dryer"/\> \<appliance wattage="1200" name="Dishwasher "/\> \<appliance wattage="785" name="Dehumidifier"/\> \<appliance wattage="100" name="Electric blanket Double"/\> \<appliance wattage="65" name="Ceiling Fan"/\> \<appliance wattage="1200" name="Hair dryer"/\> \<appliance wattage="750" name="Heater (portable)"/\> \<appliance wattage="1000" name="Clothes iron"/\> \<appliance wattage="750" name="Microwave oven"/\> \<appliance name="Personal computer"/\> \<appliance wattage="120" name="CPU - awake"/\> \<appliance wattage="30" name="CPU - asleep"/\> \<appliance wattage="150" name="Monitor - awake"/\> \<appliance wattage="30" name="Monitor - asleep"/\> \<appliance wattage="50" name="Laptop"/\> \<appliance wattage="300" name="Radio (stereo)"/\> \<appliance wattage="725" name="Refrigerator (frost-free, 16 cubic feet)"/\> \<appliance wattage="95" name="19" Televisions"/\> \<appliance wattage="113" name="27" Televisions"/\> \<appliance wattage="133" name="36" Televisions"/\> \<appliance wattage="170" name="53" - 61" Televisions"/\> \<appliance wattage="800" name="Toaster "/\> \<appliance wattage="17" name="VCR"/\> \<appliance wattage="20" name="DVD"/\> \<appliance wattage="1000" name="Vacuum cleaner"/\> \</appliances\>

Are there any tutorials on how to read an XML file like this? As I said, i followed the tutorial perfectly but it wouldn’t load due to a nil value. 

XML support is pretty weak. Can you get your data as a JSON file?

Well that minus sign on line 3 is a problem.  The XML module presented in that tutorial (I’m assuming it was this one: http://www.coronalabs.com/blog/2011/07/29/how-to-use-xml-files-in-corona/), should be able to read that file just fine.   You also need a table printing function to see what it’s returning and how it’s returned.  I use something called print_r() which can be found in the community code (Named after the similar function in PHP) that dumps your table.  Here’s the one I use:

function print\_r ( t ) &nbsp;&nbsp;&nbsp; local print\_r\_cache={} &nbsp;&nbsp;&nbsp; local function sub\_print\_r(t,indent) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (print\_r\_cache[tostring(t)]) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."\*"..tostring(t)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print\_r\_cache[tostring(t)]=true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (type(t)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for pos,val in pairs(t) do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (type(val)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos.."] =\> "..tostring(t).." {") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent..string.rep(" ",string.len(pos)+6).."}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif (type(val)=="string") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos..'] =\> "'..val..'"') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos.."] =\> "..tostring(val)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent..tostring(t)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if (type(t)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(tostring(t).." {") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(t,"&nbsp; ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("}") &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(t,"&nbsp; ") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; print() end

But like @no2games said, Lua’s table structure is very JSON like and our built in json.decode and json.encode API calls work really well.  XML because it’s not key-value pair based. While each tag can have multiple key-value attributes, you have to deal with the data that exists in between the tags.

Give the print_r function a try and see what you’re actually parsing out.

XML support is pretty weak. Can you get your data as a JSON file?

Well that minus sign on line 3 is a problem.  The XML module presented in that tutorial (I’m assuming it was this one: http://www.coronalabs.com/blog/2011/07/29/how-to-use-xml-files-in-corona/), should be able to read that file just fine.   You also need a table printing function to see what it’s returning and how it’s returned.  I use something called print_r() which can be found in the community code (Named after the similar function in PHP) that dumps your table.  Here’s the one I use:

function print\_r ( t ) &nbsp;&nbsp;&nbsp; local print\_r\_cache={} &nbsp;&nbsp;&nbsp; local function sub\_print\_r(t,indent) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (print\_r\_cache[tostring(t)]) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."\*"..tostring(t)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print\_r\_cache[tostring(t)]=true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (type(t)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for pos,val in pairs(t) do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (type(val)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos.."] =\> "..tostring(t).." {") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent..string.rep(" ",string.len(pos)+6).."}") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif (type(val)=="string") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos..'] =\> "'..val..'"') &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent.."["..pos.."] =\> "..tostring(val)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(indent..tostring(t)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if (type(t)=="table") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(tostring(t).." {") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(t,"&nbsp; ") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("}") &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sub\_print\_r(t,"&nbsp; ") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; print() end

But like @no2games said, Lua’s table structure is very JSON like and our built in json.decode and json.encode API calls work really well.  XML because it’s not key-value pair based. While each tag can have multiple key-value attributes, you have to deal with the data that exists in between the tags.

Give the print_r function a try and see what you’re actually parsing out.