Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
agmark
Visitor

Atom feeds

I've encountered an Atom feed that I'd like to parse and extract the data. I'm familiar with parsing an RSS feed, but haven't used Atom feeds. If you've worked with Atom feeds, I'd appreciate any help you can offer. If you have any sample code showing how you're parsing a feed would help a lot. I'm assuming it's similar to parsing an rss 2.0 feed, but there must be differences because so far I haven't been successful in bringing any data in.

Thanks!
~Mark
0 Kudos
2 REPLIES 2
RokuJoel
Binge Watcher

Re: Atom feeds

Here's a quickly hacked together parse of the Wikipedia example of an Atom (which I saved to a file named feed.xml)

http://en.wikipedia.org/wiki/Atom_(standard)#Example_of_an_Atom_1.0_feed

sub main()

feed=readasciifile("pkg:/feed.xml")
xml=createobject("roXMLElement")
obj=createobject("roAssociativeArray")
if xml.parse(feed) then
obj.title=xml.title.gettext()
obj.subtitle=xml.subtitle.gettext()
obj.links=[]
for each link in xml.link
obj.links.push(link.getattributes())
end for
obj.id=xml.id.gettext()
obj.updated=xml.updated.gettext()
for each element in xml.author.getchildelements()
obj[element.getname()]=element.gettext()
end for
entry=xml.entry
obj["element"]=[]

for each item in entry.getchildelements()
arr={}
arr[item.getname()]=element.gettext()
arr[item.getname()+"_attribute"]=item.getattributes()
obj.element.push(arr)
end for
else
?"parse failed"
end if
stop

end sub


It manages, through a variety of ways, to get (I think) all the attributes into a variable of one sort or another.

One way I didn't include in this example, if I just wanted the href part of a link, I could have done:

		for each link in xml.link
obj.links.push(link@href)
end for


When the code finishes running, print out the variable obj:

some examples:

BrightScript Debugger> print obj

name: John Doe
element: <Component: roArray>
email: johndoe@example.com
subtitle: A subtitle.
updated: 2003-12-13T18:30:02Z
id: urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6
title: Example Feed
links: <Component: roArray>

BrightScript Debugger> print obj.element

<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>
<Component: roAssociativeArray>

BrightScript Debugger> ?obj.element[0]

title: johndoe@example.com
title_attribute: <Component: roAssociativeArray>

BrightScript Debugger> ?obj.element[1]

link: johndoe@example.com
link_attribute: <Component: roAssociativeArray>

BrightScript Debugger> ?obj.element[1].link_attribute
href: http://example.org/2003/12/13/atom03

BrightScript Debugger> ?obj.links
http://example.org/feed/
http://example.org/


0 Kudos
agmark
Visitor

Re: Atom feeds

Thanks again Joel. That looks like a very good example. It'll give me a good start on trying to parse an atom feed. I appreciate you throwing that together and posting it. I'll play with it and let you know if I have any questions.

~Mark
0 Kudos