Parse RSS files?

Thanks! it works without a single problem now! :slight_smile:

Are there any restrictions on usage of this template? [import]uid: 24111 topic_id: 19980 reply_id: 78144[/import]

I would be bothered if you repackaged it and sold it in a tool set without throwing me something. But you can use it to build your apps anyway you see fit.
[import]uid: 19626 topic_id: 19980 reply_id: 78151[/import]

Nice thanks! :slight_smile:
Then maybe you will see your name in the credits in one of my future apps! :smiley: [import]uid: 24111 topic_id: 19980 reply_id: 78154[/import]

Tested some different rss sites and some of them have one tiny (really tiny) problem, they miss the last char. :slight_smile:

This is one of the feeds that got the problem: http://www.joystiq.com/rss.xml

Edit:

Here is another: http://feeds.feedburner.com/Feber [import]uid: 24111 topic_id: 19980 reply_id: 78165[/import]

What do you mean by β€œLast Char”, of each item? The item’s description? The last char of the entire file?
[import]uid: 19626 topic_id: 19980 reply_id: 78168[/import]

Oops maybe should have told you that :stuck_out_tongue:
The title of each story is missing the last letter/number/symbol [import]uid: 24111 topic_id: 19980 reply_id: 78170[/import]

Both of those feeds have the title embedded in a tag to protect HTML text or other character data that would look like malformed XML to the RSS namespace.

Looks like it’s in the xml.lua file line 109

top.value = (top.value or β€œβ€)…xmlText:sub(abs,cdata_end-2)

change the -2 to a -1 and see how that works.

[import]uid: 19626 topic_id: 19980 reply_id: 78181[/import]

Thank you! :slight_smile:
But can that screw up other feeds? Or can I set an if statement that chooses -1 or -2 [import]uid: 24111 topic_id: 19980 reply_id: 78184[/import]

It only affects RSS tags that contain CDATA tags in side them. This was likely shorting a character previously. I’ve written to the author of xml.lua for advice, but for right now, lets see how it behaves for you.

[import]uid: 19626 topic_id: 19980 reply_id: 78186[/import]

I added some swedish letters to the xml file cause it seemed to have some problems with those characters on some sites. The problem was that if I printed all the story titles to the terminal it replaced them with a ? but if I used display.newText to print them on the screen, it left a blank row instead.

So I added them to the UTF-8 list but it didn’t helt at all :confused: Any ideas?

My xml.lua file:

[code]

module(…, package.seeall)



–
– xml.lua - XML parser for use with the Corona SDK.

– version: 1.0

– NOTE: This is a modified version of Alexander Makeev’s Lua-only XML parser
– found here: http://lua-users.org/wiki/LuaXml



function newParser()

local DEBUG = false

XmlParser = {};

function XmlParser:ToXmlString(value)
value = string.gsub (value, β€œ&”, β€œ&”); – β€˜&’ -> β€œ&”
value = string.gsub (value, " β€œ<”
value = string.gsub (value, β€œ>”, β€œ>”); – β€˜>’ -> β€œ>”
–value = string.gsub (value, β€œβ€™β€, β€œβ€™β€); – β€˜β€™β€™ -> β€œβ€™β€
value = string.gsub (value, β€œβ€", β€œβ€"); – β€˜"’ -> β€œβ€"
– replace non printable char -> " "
value = string.gsub(value, β€œ([^%w%&%;%p%\t%])”,
function Β©
return string.format("%X;", string.byteΒ©)
–return string.format("%02X;", string.byteΒ©)
–return string.format("%02d;", string.byteΒ©)
end);
return value;
end

function XmlParser:FromXmlString(value)
value = string.gsub(value, β€œ([%x]+)%;”,
function(h)
return string.char(tonumber(h,16))
end);
value = string.gsub(value, β€œ([0-9]+)%;”,
function(h)

– hack to deal with UTF-8 entities

if h == β€œ8217” or h == β€œ8216” or h == β€œ8218” then return β€œβ€™β€ end
if h == β€œ8211” then return β€œ-” end
if h == β€œ8212” then return β€œβ€“β€ end
if h == β€œ8230” then return β€œβ€¦β€ end
if h == β€œ8220” or h == β€œ8221” or h == β€œ8222” then return β€œβ€" end
if h == β€œ8214” then return β€œβ„’β€ end
if h == β€œ8226” then return β€œ.” end
if h == β€œ229” then return β€œΓ₯” end
if h == β€œ197” then return β€œΓ…β€ end
if h == β€œ228” then return β€œΓ€β€ end
if h == β€œ196” then return β€œΓ„β€ end
if h == β€œ246” then return β€œΓΆβ€ end
if h == β€œ214” then return β€œΓ–β€ end

if tonumber(h) >= 1000 then return β€œβ€ end
return string.char(tonumber(h,100))
end);
value = string.gsub (value, β€œβ€", β€œβ€");
value = string.gsub (value, β€œβ€™β€, β€œβ€™β€);
value = string.gsub (value, β€œ>”, β€œ>”);
value = string.gsub (value, β€œ<”, " value = string.gsub (value, β€œ&”, β€œ&”);
return value;
end

function XmlParser:ParseArgs(s)
local arg = {}
string.gsub(s, β€œ(%w+)=([”’])(.-)%2", function (w, _, a)
arg[w] = self:FromXmlString(a);
end)
return arg
end

function log(…)
if DEBUG then
print("XmlParser: ", unpack(arg))
end
end

function XmlParser:ParseXmlText(xmlText)
local stack = {}
local top = {name=nil,value=nil,properties={},child={}}
table.insert(stack, top)
local ni,c,label,xarg, empty,cdata_end,cdata_end2,abs
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty =
xmlText:find("", i)
if not ni then break end

local text = string.sub(xmlText, i, ni-1);

local cdata_start,cdata_start2 = text:find(" if (cdata_start) then
abs = i+cdata_start2
log(β€œFound CDATA start tag”, abs)
cdata_end,cdata_end2 = xmlText:find("%]%]>",i)
log(β€œFound CDATA end tag”, cdata_end2)
i = cdata_end2+1
else
i = j+1
end

if not cdata_start and not string.find(text, β€œ^%s*$”) then – skip white space
top.value=(top.value or β€œβ€)…self:FromXmlString(text);
end

if cdata_start then – CDATA
top.value = (top.value or β€œβ€)…xmlText:sub(abs,cdata_end-1)
elseif empty == β€œ/” then – empty element tag
table.insert(top.child, {name=label,value=nil,properties=self:ParseArgs(xarg),child={}})
elseif c == β€œβ€ then – start tag
top = {name=label, value=nil, properties=self:ParseArgs(xarg), child={}}
table.insert(stack, top) – new level
log(β€œopenTag =”…top.name);
else – end tag
local toclose = table.remove(stack) – remove top
log(β€œcloseTag=”…toclose.name);
top = stack[#stack]
if #stack < 1 then
error("XmlParser: nothing to close with "…label)
end
if toclose.name ~= label then
error("XmlParser: trying to close with ")
end
table.insert(top.child, toclose)
end

end
local text = string.sub(xmlText, i);
if not string.find(text, β€œ^%s*$”) then
stack[#stack].value=(stack[#stack].value or β€œβ€)…self:FromXmlString(text);
end
if #stack > 1 then
error("XmlParser: unclosed "…stack[stack.n].name)
end

–local xml_obj = XmlObject(stack[1].child[1])

return stack[1].child[1];
end

function XmlParser:loadFile(xmlFilename, base)
if not base then
base = system.ResourceDirectory
end

local path = system.pathForFile( xmlFilename, base )
local hFile, err = io.open(path,β€œr”);

if hFile and not err then
local xmlText=hFile:read("*a"); – read file content
io.close(hFile);
return self:ParseXmlText(xmlText),nil;
else
print( err )
return nil
end
end

return XmlParser
end

[/code] [import]uid: 24111 topic_id: 19980 reply_id: 78306[/import]

Hi

I took youf file but don’t work

C:Runtime error: error loading module β€˜rss’ from file β€˜c:\documents an
ings\erasmo\desktop\rssgiralatina\rss.lua’:
c:\documents and settings\erasmo\desktop\rssgiralatina\rss.lua:27: β€˜)’ e
d near '"); – β€˜"’
traceback:
C: ?
C: ?
C: in function 'r

[import]uid: 100428 topic_id: 19980 reply_id: 95445[/import]

Can you post your rss.lua file in between < code> and < /code> tags (leaving out the spaces?) in its entirety?
[import]uid: 19626 topic_id: 19980 reply_id: 95729[/import]

Now
It’s works

Thanks [import]uid: 100428 topic_id: 19980 reply_id: 95735[/import]