Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
scaper12123
Level 7

How do I get XML information?

First let me preface this by saying that I have absolutely no prior experience in Brightscript or working with XML. I am trying to build a roku app that plays videos given to it by an online XML file managed by my church. Trouble is, I can't seem to get the information from it.

Here is what the XML file looks like (in general):
<ArrayOfMediaModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/BlueBridgeIntegration.Models">
<MediaModel>
<Archive_ID>6</Archive_ID>
<Archive_Title>Main</Archive_Title>
<Book>[a biblical book goes here]</Book>
<ChapterVerse>[chapter and verse go here]</ChapterVerse>
<Description>[description goes here]</Description>
<Image_Path>[link to image]</Image_Path>
<Large_Image_Path>[another link to image]</Large_Image_Path>
<MP3>[a true/false statement goes here]</MP3>
<MP4>[a true/false statement goes here]</MP4>
<Media_ID>9212</Media_ID>
<MessageDate>[date goes here]</MessageDate>
<Message_Number>20160622</Message_Number>
<Notes>[a long set of notes goes here]</Notes>
<PDF>[true/false statement]</PDF>
<RTMP_Path>/main/mp4</RTMP_Path>
<Series_ID>111</Series_ID>
<Series_Title>[a title goes here]</Series_Title>
<Spanish_Notes i:nil="true"/>
<Spanish_Title i:nil="true"/>
<Speaker>[a name goes here]</Speaker>
<SubTitle i:nil="true"/>
<Title>[a title goes here]</Title>
<audioFile i:nil="true"/>
<audioFileRTMP i:nil="true"/>
<videoFile i:nil="true"/>
<videoFileRTMP i:nil="true"/>
</MediaModel>
<MediaModel>...</MediaModel>
<MediaModel>...</MediaModel>
<MediaModel>...</MediaModel>
</ArrayOfMediaModel>


And here is the code I'm trying to use to get this information from the file (which, again, is only available online in this context):
sub CreateRecentMenu()
screen = CreateObject("roGridScreen")
port = CreateObject("roMessagePort")
xml = CreateObject("roXMLElement")
xml_str = CreateObject("roString")

xml_str = GetXML("[url goes here]")

if xml.Parse(xml_str) then
For Each MediaModel in ArrayOfMediaModel
print "media" 'this is essentially a debugging statement. The Parse() function fails and "failure to parse" prints instead
end For
else
print "failure to parse"
end if

'I have more code beyond this point, but i have it all commented out for the sake of fixing the XML problem
end sub

Function GetXML(url as String) as string
link = CreateObject("roUrlTransfer")
link.setUrl(url)
link.SetCertificatesFile("common:/certs/ca-bundle.crt")
link.InitClientCertificates()

return link.GetToString()
end function


As I stated in the code, the Parse() function fails and the if statement always prints "failure to parse." My attempts to find a solution thus far have proven inadequate and I have tried a few work-arounds, but to no end. As I said, I have no experience and thus I have no idea what the problem could possibly be. Any help pointing out the issue would be greatly, very much appreciated. Thank you.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
10 Replies
belltown
Level 7

Re: How do I get XML information?

You can use an XML validator to determine the validity of your XML:

https://validator.w3.org/
http://www.w3schools.com/xml/xml_validator.asp
http://codebeautify.org/xmlvalidate

Also, your code doesn't do any error-checking when you read the url, so there's no way to know what was actually read, if anything. At the very least, use the debugger or a Print statement to see what exact string is passed to the Parse() method.
https://github.com/belltown/
0 Kudos
scaper12123
Level 7

Re: How do I get XML information?

"belltown" wrote:
your code doesn't do any error-checking when you read the url, so there's no way to know what was actually read, if anything. At the very least, use the debugger or a Print statement to see what exact string is passed to the Parse() method.


I have done that, and as far as I can guess it seems to pull out the XML data just fine. However, the string doesn't have the XML verbatim (with all the <> </> things and stuff). Again, I don't have much experience so I don't really know what to look for. That's my biggest problem here.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
belltown
Level 7

Re: How do I get XML information?

It parses okay on my Roku. Try copying the XML to a file in your package and read it from there instead of from the network and see if it works. This works for me:


Sub Main ()
xml = CreateObject ("roXmlElement")
data = ReadAsciiFile("pkg:/xml/data.xml")
Print data
result = xml.Parse(data)
Print result
Stop
End Sub



<ArrayOfMediaModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/BlueBridgeIntegration.Models">
<MediaModel>
<Archive_ID>6</Archive_ID>
<Archive_Title>Main</Archive_Title>
<Book>[a biblical book goes here]</Book>
<ChapterVerse>[chapter and verse go here]</ChapterVerse>
<Description>[description goes here]</Description>
<Image_Path>[link to image]</Image_Path>
<Large_Image_Path>[another link to image]</Large_Image_Path>
<MP3>[a true/false statement goes here]</MP3>
<MP4>[a true/false statement goes here]</MP4>
<Media_ID>9212</Media_ID>
<MessageDate>[date goes here]</MessageDate>
<Message_Number>20160622</Message_Number>
<Notes>[a long set of notes goes here]</Notes>
<PDF>[true/false statement]</PDF>
<RTMP_Path>/main/mp4</RTMP_Path>
<Series_ID>111</Series_ID>
<Series_Title>[a title goes here]</Series_Title>
<Spanish_Notes i:nil="true"/>
<Spanish_Title i:nil="true"/>
<Speaker>[a name goes here]</Speaker>
<SubTitle i:nil="true"/>
<Title>[a title goes here]</Title>
<audioFile i:nil="true"/>
<audioFileRTMP i:nil="true"/>
<videoFile i:nil="true"/>
<videoFileRTMP i:nil="true"/>
</MediaModel>
<MediaModel>
<Archive_ID>6</Archive_ID>
<Archive_Title>Main</Archive_Title>
<Book>[a biblical book goes here]</Book>
<ChapterVerse>[chapter and verse go here]</ChapterVerse>
<Description>[description goes here]</Description>
<Image_Path>[link to image]</Image_Path>
<Large_Image_Path>[another link to image]</Large_Image_Path>
<MP3>[a true/false statement goes here]</MP3>
<MP4>[a true/false statement goes here]</MP4>
<Media_ID>9212</Media_ID>
<MessageDate>[date goes here]</MessageDate>
<Message_Number>20160622</Message_Number>
<Notes>[a long set of notes goes here]</Notes>
<PDF>[true/false statement]</PDF>
<RTMP_Path>/main/mp4</RTMP_Path>
<Series_ID>111</Series_ID>
<Series_Title>[a title goes here]</Series_Title>
<Spanish_Notes i:nil="true"/>
<Spanish_Title i:nil="true"/>
<Speaker>[a name goes here]</Speaker>
<SubTitle i:nil="true"/>
<Title>[a title goes here]</Title>
<audioFile i:nil="true"/>
<audioFileRTMP i:nil="true"/>
<videoFile i:nil="true"/>
<videoFileRTMP i:nil="true"/>
</MediaModel>
</ArrayOfMediaModel>



------ Running dev 'Junk' main ------
<ArrayOfMediaModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/BlueBridgeIntegration.Models">
<MediaModel>
<Archive_ID>6</Archive_ID>
<Archive_Title>Main</Archive_Title>
<Book>[a biblical book goes here]</Book>
<ChapterVerse>[chapter and verse go here]</ChapterVerse>
<Description>[description goes here]</Description>
<Image_Path>[link to image]</Image_Path>
<Large_Image_Path>[another link to image]</Large_Image_Path>
<MP3>[a true/false statement goes here]</MP3>
<MP4>[a true/false statement goes here]</MP4>
<Media_ID>9212</Media_ID>
<MessageDate>[date goes here]</MessageDate>
<Message_Number>20160622</Message_Number>
<Notes>[a long set of notes goes here]</Notes>
<PDF>[true/false statement]</PDF>
<RTMP_Path>/main/mp4</RTMP_Path>
<Series_ID>111</Series_ID>
<Series_Title>[a title goes here]</Series_Title>
<Spanish_Notes i:nil="true"/>
<Spanish_Title i:nil="true"/>
<Speaker>[a name goes here]</Speaker>
<SubTitle i:nil="true"/>
<Title>[a title goes here]</Title>
<audioFile i:nil="true"/>
<audioFileRTMP i:nil="true"/>
<videoFile i:nil="true"/>
<videoFileRTMP i:nil="true"/>
</MediaModel>
<MediaModel>
<Archive_ID>6</Archive_ID>
<Archive_Title>Main</Archive_Title>
<Book>[a biblical book goes here]</Book>
<ChapterVerse>[chapter and verse go here]</ChapterVerse>
<Description>[description goes here]</Description>
<Image_Path>[link to image]</Image_Path>
<Large_Image_Path>[another link to image]</Large_Image_Path>
<MP3>[a true/false statement goes here]</MP3>
<MP4>[a true/false statement goes here]</MP4>
<Media_ID>9212</Media_ID>
<MessageDate>[date goes here]</MessageDate>
<Message_Number>20160622</Message_Number>
<Notes>[a long set of notes goes here]</Notes>
<PDF>[true/false statement]</PDF>
<RTMP_Path>/main/mp4</RTMP_Path>
<Series_ID>111</Series_ID>
<Series_Title>[a title goes here]</Series_Title>
<Spanish_Notes i:nil="true"/>
<Spanish_Title i:nil="true"/>
<Speaker>[a name goes here]</Speaker>
<SubTitle i:nil="true"/>
<Title>[a title goes here]</Title>
<audioFile i:nil="true"/>
<audioFileRTMP i:nil="true"/>
<videoFile i:nil="true"/>
<videoFileRTMP i:nil="true"/>
</MediaModel>
</ArrayOfMediaModel>
true

Current Function:
001: Sub Main ()
002: xml = CreateObject ("roXmlElement")
003: data = ReadAsciiFile("pkg:/xml/data.xml")
004: Print data
005: result = xml.Parse(data)
006: Print result
007:* Stop
008: End Sub
STOP (runtime error &hf7) in pkg:/source/Main.brs(7)
007: Stop
Backtrace:
#0 Function main() As Void
file/line: pkg:/source/Main.brs(8)
Local Variables:
global rotINTERFACE:ifGlobal
m roAssociativeArray refcnt=2 count:0
xml bsc:roXMLElement refcnt=1
data roString (2.1 was String) refcnt=1 val:"<ArrayOfMediaModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.d"...
result Boolean val:true

BrightScript Debugger>
https://github.com/belltown/
0 Kudos
scaper12123
Level 7

Re: How do I get XML information?

Argh... infuriatingly enough, I was given an error on attempting to install, telling me it couldn't parse the xml file (i copied it verbatim from your post to test it). Now I'm just utterly confused... it also said "see port 8089 for details" which i can't seem to figure out how to look at. Smiley Sad

I had one thought though. If I used AsyncGetToFile(), would I be able to extract the information from the xml file and paste it into a file verbatim?

EDIT: is there any way you can share the zip application you made? The one that you said had the XML and was able to work on your Roku? It may be possible that my system is doing something weird to the XML when I try to compile the zip. I'd like to confirm or deny that possibility.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
belltown
Level 7

Re: How do I get XML information?

"scaper12123" wrote:
is there any way you can share the zip application you made? The one that you said had the XML and was able to work on your Roku? It may be possible that my system is doing something weird to the XML when I try to compile the zip. I'd like to confirm or deny that possibility.

https://github.com/belltown/junk
https://github.com/belltown/
0 Kudos
scaper12123
Level 7

Re: How do I get XML information?

"belltown" wrote:
https://github.com/belltown/junk


OK, I was able to get that file running. I guess the challenge becomes first finding out what the difference between your files and mine were so I can sort out what problem I had with that xml in the first place, and then seeing if I can apply whatever I find there to the files I have now. I appreciate your help.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
belltown
Level 7

Re: How do I get XML information?

It could be an encoding issue. Make sure the XML file is encoded in UTF-8 (or a subset thereof), with NO byte order mark.
https://github.com/belltown/
0 Kudos
scaper12123
Level 7

Re: How do I get XML information?

"belltown" wrote:
It could be an encoding issue. Make sure the XML file is encoded in UTF-8 (or a subset thereof), with NO byte order mark.


I've tried a few things to sort out the issue between the documents but, strangely enough, I have come to the conclusion that it is the file itself rather than anything in the file that is causing the problem. Again, tried copy-pasting the example you had (even after i confirmed that there was no difference between it and the file I had) and it still gave me the same error. I may have to do some more fiddling and post about what results I get; it's really odd.
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos
scaper12123
Level 7

Re: How do I get XML information?

I've had a little look over my code and, after some experimentation, was able to get it to work with another xml document. Trouble is it won't work with the one i'm using. Is the header (<?xml version="1.0" encoding="utf-8" ?>) absolutely required for the GetToString() function to grab anything from the file?
I AM THE ARCHMAGE... who is also rather new to Brightscript so forgive me for seeming inept at times.
0 Kudos