XML API Feature

Hello, I'd like to make the request for an XML API to support easy loading and parsing of XML. I'm looking to use this with Corona Game Edition because I'll be writing a game editor tool that will write out XML data that must then be read into the game. The current solutions seem overly time consuming or complex and having an easy to use API in Corona would be a great help.

Here are some other threads asking for essentially the same thing:

http://developer.anscamobile.com/forum/2010/04/06/e4x-style-xml-parsing

http://developer.anscamobile.com/forum/2010/04/28/xml-json-ini-parser-support

Whatever API is provided I'd like to see something similar to E4X that makes it easy to load game data and make changes to the code even as the XML schema changes through the development of the game editor and the game.

Couldn't agree more. A basic xml api is a necessity in a game engine.

Although not quite an API we use http://lua-users.org/wiki/LuaXml

We are looking at implementing a Corona XML framework.

Carlos

Thanks I'll give this a try for now although I can see us outgrowing this rather quickly. A Corona XML framework as you mention is going to be needed for larger projects. Maybe you can adapt some of the code from LuaXML:
http://www.viremo.de/LuaXML/

They are using the MIT license so that might worth looking at.

AFonseca

We are looking at bigger picture. As a matter of fact, as a side note, one of our Board Advisors who used to work with me at Adobe, wrote his own XML parser for his Corona project and once he finishes his app, he will end up giving us his XML parser. I havent' seen the code, so I can't comment as to what it can and can't do, but knowing him....

We will look at the LuaXML you mention.

Thanks

Carlos

Thanks for keeping us up to date, that sounds great. Hopefully he'll be done soon so we don't have to invest too much time/effort in using external libraries.

I'll check out the link provided above, but I'd like to second the request for builtin XML support.

hi any news on this, is it implemented or still planned?

I'll be writing a game editor tool that will write out XML data that must then be read into the game.

I do this very thing using Maya for level editing (ie. I wrote a Python script that dumps out an XML file of all the objects in the scene.) That's for 3D but I'll probably do the same thing to place things in 2D.

Any news on the API? I really would like to see an E4X-Like approach in Corona. We're developing apps and looking for a multiscreening approach (which adobe failed up to now with its approach in flash) but need heavy XML-support.

I have yet to try the LuaXML script though... but it seems a bit complicated and limited to me.

Just thought I would quickly chime in as well as I'm looking to do a few apps, game and non-game that would use XML rather heavily and having something like http://viremo.eludi.net/LuaXML/ in an API would be a god send for what I'm looking to do. So count this as a +1 for a robust XML API.

Hi,

We have been looking at LuaXML and it seems there are now more questions than answers concerning lua XML parsing. A Corona XML framework would be a very welcome and essential feature. So count this as +1 as well.

Additionally a Corona web service (SOAP) framework would be a brilliant feature. Our games use web services extensively and we are struggling to implement a reliable and easy-to-use solution in Corona as yet.

Many thanks for all your work.

this looks interesting
http://www.birtles.org.uk/xmltv/wiki/index.php/Xml_to_table

resulting in code like

1
2
3
aentre.dvddata = XmlToTable:ParseFile(file)
 
for i, dvd in ipairs(aentre.dvddata.Children.Collection[1].Children.DVD) do

+1 on implemented xml support in Corona!

This example takes me forward: http://developer.anscamobile.com/code/weather-app-example but I'm still new at this so i make it in ant-steps.

Easer to create a physics game than to read a xml file and do the right things with the information. :)

Hi,

As I have struggled with XML parsing in Lua, I thought I would share the functions I am now using. It is by far not a great solution but at least everything is written in Lua (no libraries). I believe this code was originally posted (and maybe even written) by Carlos. As I struggled to find it I thought I post it here:

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local http = require("socket.http")
local io = require("io");
local ltn12 = require("ltn12")
 
function parseargs(s)
        local arg = {}
        string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
        arg[w] = a
        end)
        return arg
end
 
function parseXML(s)
 
        local stack = {}
        local top = {}
        table.insert(stack, top)
        local ni, c, label, xarg, empty
        local i, j = 1, 1
        while true do
                ni, j, c, label, xarg, empty = string.find(s, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
                if not ni then break end
                local text = string.sub(s, i, ni-1)
                if not string.find(text, "^%s*$") then
                        table.insert(top, text)
                end
                if empty == "/" then -- empty element tag
                        table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
                elseif c == "" then -- start tag
                        top = {label=label, xarg=parseargs(xarg)}
                        table.insert(stack, top) -- new level
                else -- end tag
                        local toclose = table.remove(stack) -- remove top
                        top = stack[#stack]
                        if #stack < 1 then
                                error("nothing to close with "..label)
                        end
                        if toclose.label ~= label then
                                error("trying to close "..toclose.label.." with "..label)
                        end
                        table.insert(top, toclose)
                end
                i = j+1
        end
        local text = string.sub(s, i)
        if not string.find(text, "^%s*$") then
                table.insert(stack[#stack], text)
        end
        if #stack > 1 then
                error("unclosed "..stack[stack.n].label)
        end
        return stack[1]
end
 
b = http.request( "http://www.w3schools.com/ajax/note.xml") -- blah blah use your own url
 
x = parseXML(b); -- parse xml
 
-- Do whatever you want with the data...
print("To:", x[2][1][1])
print("From:", x[2][2][1])
print("Message:", x[2][4][1])

Thanks to whomever wrote what nanonil posted. I modified it for an associative table structure. It's not comprehensive, but seems ok for simple XML parsing.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--usage
local xmlParser = require "xml"
xml = xmlParser.parseXML( "<stuff><title>MyTitle</title><items><item guid='1xkj34l'>item1</item><item guid='1x3fjd2'>item2</item></items></stuff>" )
print( xml.stuff.items.item[1].value )
print( xml.stuff.items.item[1].guid )
 
--the module
module(..., package.seeall)
 
local function loadAttributes( element, s )
        string.gsub( s, "(%w+)=([\"'])(.-)%2", function ( w, _, a )
        element[w] = a
        end )
end
 
function parseXML( s )
        local stack = {}
        local top = {}
        table.insert( stack, top )
        local ni, c, label, xarg, empty
        local i, j = 1, 1
        while true do
                ni, j, c, label, xarg, empty = string.find(s, "<(%/?)([%w_:]+)(.-)(%/?)>", i)
                if not ni then break end
                local text = string.sub(s, i, ni-1)
                if not string.find(text, "^%s*$") then
                                        top.value = text
                end
                                
                if empty == "/" then -- empty element tag
                                        loadAttributes( top, xarg )
                                                
                elseif c == "" then -- start tag
                                        top = {}
                                        loadAttributes( top, xarg )
                    table.insert( stack, top )
                                          
                else -- end tag
                                        local close = table.remove( stack )
                                        top = stack[ #stack ]
                                        
                                        if top[ label ] then
                                                if #top[ label ] ~= 0 then
                                                        table.insert( top[ label ], close )
                                                else
                                                        local node = top[ label ]
                                                        local collection = {}
                                                        table.insert( collection, node )
                                                        table.insert( collection, close )
                                                        top[ label ] = collection
                                                end
                                        else 
                                                top[ label ] = close
                                        end                                     
                end
                i = j+1
        end
        return stack[1]
end

with regard to singh206 update

if you need to get the number of items

print( table.getn(xml.stuff.items.item) )

Any word on an official XML parser as part of the Corona SDK? We're creeping up on a year since my original post. :)

views:2246 update:2011/9/18 10:09:28
corona forums © 2003-2011