I’m trying to read in an xml file and parse it. I see an XmlParser in CoronaUI, but apparently I’m not using it right:
local coronaui = require( "coronaui" )
local xmlParser = coronaui.newXmlParser()
local xml = xmlParser:ParseXmlFile("level1.xml")
for i,val in ipairs(xml) do
print(i,val);
end
Nothing gets printed. What am I doing wrong? [import]uid: 52127 topic_id: 9096 reply_id: 309096[/import]
Lua provides a pairs() function to create the explist information for us to iterate over a table. The pairs() function will allow iteration over key-value pairs. Note that the order that items are returned is not defined, not even for indexed tables.
The ipairs() function will allow iteration over index-value pairs. These are key-value pairs where the keys are indices into an array. The order in which elements are returned is guaranteed to be in the numeric order of the indices, and non-integer keys are simply skipped. Using the same table as in the example above:
remove the i in ipairs should be pairs !!!
c [import]uid: 24 topic_id: 9096 reply_id: 33176[/import]
Finally figured this out. To get at the player node in my example you have to do this:
xml.ChildNodes[2].Value
Which is not very readable or easy to work with. With a more complex XML file it will involve a lot of traversing through child nodes and hard-coding index values.
We would really benefit from an E4X XML solution, like AS3 does it for example.
[code]
local function foo (me, value,myV )
for k,v in pairs (me) do
if ( value == tostring(k))then
myValue = tostring(v);
return myValue;
elseif ( type (v) == “table” )then
myValue = foo(v,value,myValue);
end
end
return myValue
end
local city = foo ( xml, “whatevernode”);
[/code] [import]uid: 24 topic_id: 9096 reply_id: 33267[/import]
Thanks for the suggestion. In your example city is always nil though. I’m not sure what “myV” is supposed to be (the 3rd parameter of the foo function). On line 13 you call foo with only 2 parameters, but on line 7 you call it with 3. [import]uid: 52127 topic_id: 9096 reply_id: 33271[/import]
I’m having a terrible time trying to parse an rss feed from the web. I’ve tried all sorts of xml parsers off of the lua website, and the coronaui one as well (though that one at least gave me errors). I’m trying to convert this stuff to a table. Also, this rss feed is not something I can readily adjust from the other end. I have to take whatever is being sent to me and configure it from corona.
I’m using some code I got off of the forum or code exchange in conjunction with the coronaui parser.
here’s what I have so far:
[lua]-- load needed modules
local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
– a simplified http.get function
local function httpget(u)
local t = {}
local r = http.request{
url = u,
sink = ltn12.sink.table(t)
}
return t
end
local function dump(o)
if type(o) == ‘table’ then
local s = ‘{ ’
for k,v in pairs(o) do
if type(k) ~= ‘number’ then k = ‘"’…k…’"’ end
s = s … ‘[’…k…’] = ’ … dump(v) … ‘,’
end
return s … '} ’
else
return tostring(o)
end
end
local temp2 = xmlParser:ParseXmlText(dump(x))[/lua]
if I use xmlParser:FromXmlString(dump(x)), then it runs, but I just end up with one long string instead of a table.
and then I get this message in the terminal:
XmlParser: trying to close br with description
XmlParser: trying to close br with item
XmlParser: trying to close br with description
XmlParser: trying to close br with item
XmlParser: trying to close br with description
XmlParser: trying to close br with item
XmlParser: trying to close br with description
XmlParser: trying to close br with item
XmlParser: trying to close br with description
XmlParser: trying to close br with item
XmlParser: trying to close description with channel
XmlParser: trying to close item with rss
Runtime error
…Svm99p2jsk+++TI/-Tmp-/TemporaryItems/41/coronaui.lua:137: attempt to index field ‘?’ (a nil value)
stack traceback:
[C]: ?
…Svm99p2jsk+++TI/-Tmp-/TemporaryItems/41/coronaui.lua:137: in function ‘ParseXmlText’
Gilbert Guerrero has an rss reader (or similar) for his ATA app and that is open source and you can look into his code. His app has been on the market since almost Jan of 2010. https://github.com/gg-ansca/ATA
c. [import]uid: 24 topic_id: 9096 reply_id: 35138[/import]