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: 
btpoole
Channel Surfer

Help Parse XML

Need some direction. I am using urlTransfer to save a file to tmp on Roku. I think need to parse the file. When I get the file it comes in looking like the following:

<?xml version="1.0" encoding="UTF-8" ?>
<schedule>
<DAY attr="2014-11-4">
<time attr="06:00 am">
<show name="My Show">
<sid>6978</sid>
<network>My Channel</network>
<title>Season 17, Episode 277</title>
<ep>17x277</ep>
<link>http://www.mytv.com/My Show/episodes/1065649497</link>
</show>
</time>
</DAY>

</schedule>


I have only pasted part of file to give idea of what it looks like. I have tried the following code to extract the network but am not getting any thing back. Not sure if I am doing this correctly. I know the file is populated. I am having trouble with which field to use for on. Do I use it on schedule, day, time? Thanks for any help


readxml=ReadAsciiFile("tmp:/status.temp")
xmlfilelist = CreateObject("roXMLElement")
if xmlFileList.Parse(readxml) then
For Each day in xmlFileList.schedule
network = day.network.GetText()
Print"NETWORK. . "network
Next
End If
0 Kudos
24 REPLIES 24
btpoole
Channel Surfer

Re: Help Parse XML

Can somebody give me an example of how to extract the values of day, time, show, and network of the following, would be greatful. Thanks

<?xml version="1.0" encoding="UTF-8" ?>
<schedule>
<DAY attr="2014-11-4">
<time attr="06:00 am">
<show name="My Show">
<sid>6978</sid>
<network>My channel</network>
<title>Season 17, Episode 277</title>
<ep>17x277</ep>
</show>
</time>
</DAY>
</schedule>
0 Kudos
RokuMarkn
Visitor

Re: Help Parse XML


x.day@attr
x.day.time@attr
x.day.time.show@name
x.day.time.show.network.GetText()


--Mark
0 Kudos
btpoole
Channel Surfer

Re: Help Parse XML

Thanks RokuMarn but I get an invalid back. The readxml is the file being parsed. When executed I get invalid, invalid, invalid,0.
Thanks again

xmlfilelist = CreateObject("roXMLElement")
xxml=xmlFileList.Parse(readxml)
if xmlFileList.Parse(readxml) then
PRINT"PASSED. . ."
day=xmlfilelist.day@attr
time=xmlfilelist.time@attr
show=xmlfilelist.day.time.show@name
network=xmlfileList.day.time.show.network.GetText()
print day
print time
print show
print network
END IF
0 Kudos
EnTerr
Roku Guru

Re: Help Parse XML

It should've worked (it surprised me a little roXml dot-ing to be case-insensitive, so i tried - and it is):
BrightScript Debugger> x.parse("<schedule><DAY attr='2014-11-4'>   <time attr='06:00 am'>         <show name='My Show'>            <sid>6978</sid>            <network>My channel</network>            <title>Season 17, Episode 277</title>            <ep>17x277</ep>         </show>       </time></DAY></schedule>")
BrightScript Debugger> ? x.day@attr
2014-11-4
BrightScript Debugger> ? x.day.time@attr
06:00 am
BrightScript Debugger> ? x.day.time.show@name
My Show
BrightScript Debugger> ? x.day.time.show.network.GetText()
My channel

Use genXML() to see what actually got parsed (e.g. ? xmlFileList.genXML(0) ).
0 Kudos
btpoole
Channel Surfer

Re: Help Parse XML

Thanks again. I found the problem. I only posted one entry from the file but the real file has multiple entries. When I ran the code using the file with only one entry I get the values. I guess I need to use array for each value and a for each to populate the array.
Thanks again
0 Kudos
btpoole
Channel Surfer

Re: Help Parse XML

Would the following be the correct way to create an array for Day in the above xml file assuming there are multiple entries? I get an error stating: Runtime: FOR EACH value is not an enumerable object. I know there are multiple "DAY" in the file.

daylist=[]
xmlfilelist = CreateObject("roXMLElement")
for each DAY in xmlFileList
daylist.Push({
DAY: {DAY: xmlfilelist.day@attr}
})
End For
0 Kudos
EnTerr
Roku Guru

Re: Help Parse XML


for each day in xmlFileList.DAY: 'this returns a list of DAYs; xmlFileList is not really a list but roXmlElement
for each time in day.time:
for each show in time.show:
'do the due
end for
end for
end for
0 Kudos
btpoole
Channel Surfer

Re: Help Parse XML

Thank you EnTerr for your help. I hate to keep requesting advice on this but I am really having difficulties. The idea is that I am creating a simple program guide. I use urlTransfer to write a file from a url that produces an xml file with program info. I get the file, write it, read it run genXML(0). The result comes back showing all the information. When I attempt to populate and array with the DAY and print it to verify it's actually getting something, I get a print for each that says <Component: roAssociativeArray>. When I first posted I had cut the xml file just to show one entry, I am able to extract that one entry but not able to perform a for each that would gather each. Am I totally missing something here? If i curl the url and save it and open in notepad++, the header shows<?xml version="1.0" encoding="UTF-8" ?>, so I assume I am getting an xml in correct format.
Thanks again

[code}
xfer=CreateObject("roUrlTransfer")
xferport=CreateObject("roMessagePort")
xfer.SetMessagePort(xferport)
xmlurl="http://services.tvrage.com/feeds/fullschedule.php?country=US"
xfer.seturl (xmlurl)
xfer.GetToFile("tmp:/status.temp")
readxml=ReadAsciiFile("tmp:/status.temp")
xmlfilelist = CreateObject("roXMLElement")
xxml=xmlFileList.Parse(readxml)
daylist=[]
if xmlFileList.Parse(readxml) then
PRINT"PASSED. . ."
for each day in xmlFileList.DAY
daylist.Push({
DAY: xmlFileList.DAY
})
Print"daylist. . . "daylist
end for
else
PRINT"DID NOT PASS"

END IF
[/code]
0 Kudos
EnTerr
Roku Guru

Re: Help Parse XML

No sweat - that's because PRINT outputs only the top level of nested structures:
BrightScript Debugger> ? {a: 1}
a: 1

BrightScript Debugger> ? [ 1 ]
1

BrightScript Debugger> ? [ {a: 1} ]
<Component: roAssociativeArray>
The last two are confusing, i agree - there is no hint that what is output is a vector and not scalar.

I am quite unclear on while you are doing that dictionary-inside-array thing though! Looking at your feed, you have a dictionary indexed-by-date, containing dictionary indexed-by-time, containing... well either list of shows or dictionary indexed-by sid or name. How you are going to ingest the data depends on how you are going to use it - so i cannot guess what you need exactly.
0 Kudos