In the Corona SDK blog, there was a recent post about parsing XML files and there are exiting libraries that make it reasonably easy.
In fact I’m building an App for my older son’s Music Blog and I’ve got it to a point where I can read the entries with podcasts and extract out the URL. I’m now stuck (translate to: being lazy) on creating a player to play it.
local xml = require( "xml" ).newParser()
local stories = {}
local networkListener = function( event )
local feed
local story = {}
if ( event.isError ) then
print ( "Network error - download failed" )
story.title = "Network Error"
story.link = nil
stories[1] = story
else
print("Parsing the feed")
feed = xml:loadFile("index.rss", system.TemporaryDirectory)
local items = feed.child[1].child
local i
local l = 1
for i = 1, #items do
local item = items[i]
if item.name == "item" then -- we have a story batman!
local j
for j = 1, #item.child do
if item.child[j].name == "title" then
story.title = item.child[j].value
end
if item.child[j].name == "link" then
story.link = item.child[j].value
end
if item.child[j].name == "pubDate" then
story.pubDate = item.child[j].value
end
if item.child[j].name == "content:encoded" then
-- get the story body
bodytag = {}
bodytag = item.child[j].child
utility.print\_r(bodytag)
local p;
story.body = ""
for p = 1, #bodytag do
if (bodytag[p].value) then
story.body = story.body .. bodytag[p].value .. "\n\n"
end
end
end
if item.child[j].name == "enclosure" then
local properties = {}
properties = item.child[j].properties
story.podcastURL = properties.url
story.podcastSize = properties.length
story.podcastType = properties.type
end
end
stories[l] = {}
stories[l].link = story.link
stories[l].title = story.title
stories[l].body = story.body
stories[l].podcastURL = story.podcastURL
stories[l].podcastSize = story.podcastSize
stories[l].podcastType = story.podcastType
-- print("[[" .. story.body .. "]]")
l = l + 1
end
end
end
end
network.download("http://yoursite.com/feed/", "GET", networkListener, "index.rss", system.TemporaryDirectory )
This seems to be working for me! I should note that I haven’t tested this well. Also the blog I’m fetching from makes heavy use of special characters for quotes, mdashes, etc. And the XML library I’m using chokes on them, so I’ve been hacking it to convert them to ASCII on the fly.
[import]uid: 19626 topic_id: 13462 reply_id: 49617[/import]