CoronaUI - XML Parser

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:

1
2
3
4
5
6
7
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

what's in your level1.xml

c

1
2
3
4
5
<?xml version="1.0" ?>
<level>
  <bg>bg1.png</bg>
  <player>player1.png</player>
</level>

whack !

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

You're right, now I get this:

1
2
3
Attributes      table: 0x4c4ba0
Name    level
ChildNodes      table: 0x4c4c20

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.

Try something like this - (code may not work)....

1
2
3
4
5
6
7
8
9
10
11
12
13
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");

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.

Hi guys,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- 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 x = httpget("http://hm.bhusd.org/apps/events/events_rss.jsp?id=1")
 
local xmlParser = coronaui.newXmlParser()
 
local temp2 = xmlParser:ParseXmlText(dump(x))

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.

where is it coronaui by download?
Pleasee

Corona UI is for Subscribers only.

C.

Yes, I'm a Subscribers Corona SDk (IOS)

Then goto the CoronaUI subscribers section. Under Corona UI there is a download link.

Carlos.

Sorry Carlos,
Where is this section?
thanks

The forum for subscribers. You should see it when you log in. Only available for paying subscribers.

C.

views:1549 update:2011/9/29 19:21:19
corona forums © 2003-2011