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

parsing XML question

I'm getting an xml feed and trying to extract some data. With tags like

<name>Joseph</name>
it works just fine.


but with With tags like:
<file-count type="Integer">30</file-count>

I get this error:

/tmp/plugin/HJAAAAREIkOr/pkg:/source/main.brs(33): runtime error ec: 'Dot' Operator attempted with invalid BrightScript Component or interface reference.

033: tcs=xml.file-count.GetText()

and:

tcs &h30 Untyped val:Uninitialized

Can anyone help? How do get the value of a tag that has subsidiary information in it like "type"? Should I declare tcs as an integer first? It doesn't seem to help. Do I need to do a test of some kind on each xml key before I attempt to assign it to a variable?

Thanks!
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
37 REPLIES 37
kbenson
Visitor

Re: parsing XML question

"-" is not a valid variable character. Use one of the alternate access methods, such as a member function, to access the element. The Reference manual mentions this when referring to the need to do case sensitive matching, so it will most likely work here as well.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
jbrave
Channel Surfer

Re: parsing XML question

Ok, looks like you are correct that the - is the cause of the problem. Now how do I get that xml tag without using the dash character - it is in the xml feed and there are many other xml tags that have that character as well
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: parsing XML question

The roXMLElement component has a GetNamedElements() method. You should be able to use that to get an roXMLList of the "file-count" elements that you can loop through.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
jbrave
Channel Surfer

Re: parsing XML question

Thanks for responding. I don't see any examples of actual usage of GetNamedElement in the documentation. I'm searching through the examples also, and the videoplayer which actually parses XML doesn't use this function. Can you post an example of how GetNamedElement is actually used?
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
hoffmcs
Visitor

Re: parsing XML question

In your case it would be:
xml.GetNamedElement("file-count")[0].GetText()
0 Kudos
jbrave
Channel Surfer

Re: parsing XML question

Thanks! So, how would one know that a function like GetNamedElement would be called like this, as opposed to xml.tagname.GetNamedElement() ?

What do I look for when reading the Developer manual to know if a function gets called like:

something.Function() or if it gets called like:

Function("something")

In other words, how did you come up with that statement and know that it works?

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
hoffmcs
Visitor

Re: parsing XML question

It is documented in the SDK. Look at section 10.16 in the Brightscript Reference Manual. There are also some examples in the generalUtil.brs files found in some of the example channels.
0 Kudos
jbrave
Channel Surfer

Re: parsing XML question

Thanks, I read the manual section you suggested again, and I understand a little bit better now. However, I still don't understand all the aspects of that code you posted. What is the [0] about, seems like an array reference, but why is it needed when there is already the name of the tag in the parenthesis:

xml.GetNamedElement("file-count")[0].GetText()

Thanks,

Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: parsing XML question

"jbrave" wrote:
Thanks, I read the manual section you suggested again, and I understand a little bit better now. However, I still don't understand all the aspects of that code you posted. What is the [0] about, seems like an array reference, but why is it needed when there is already the name of the tag in the parenthesis:

xml.GetNamedElement("file-count")[0].GetText()

Thanks,

Joel

The name of the method is actually GetNamedElements and it returns an roXMLList, so you need to get the first index of that list to get the XML element you're looking for. The safest way to do this is probably...


xmlFileCount = xml.GetNamedElements("file-count")
If xmlFileCount <> invalid And fileCount.Count() > 0 Then
fileCount = xmlFileCount[ 0 ].GetText()
End If

With that, you won't blow up if the element is missing from your XML for some reason.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.