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: 
ioan
Roku Guru

Choose a random XML element

Let's say I have this xml:

<madona>
<img src="madonna1.jpg" alt='Foligno Madonna, by Raphael' />
<img src="madonna2.jpg" alt='Madonna' />
<img src="madonna3.jpg" alt='Foligno Madonna, by Raphael' />
<img src="madonna4.jpg" alt='Foligno Madonna, by Raphael' />
</madona>

... and I want to choose an img element at random. Can someone point me in the right direction?

Thanks.
https://github.com/e1ioan/
http://rokucam.com
0 Kudos
2 REPLIES 2
renojim
Community Streaming Expert

Re: Choose a random XML element

xmlel = CreateObject("roXmlElement")
xmlel.Parse(xml) ' should check return value
rndimg = xmlel.img[rnd(xmlel.img.Count())-1]
src = rndimg@src
alt = rndimg@alt

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
EnTerr
Roku Guru

Re: Choose a random XML element

Given the example parsed into roXmlElement `xml`:

BrightScript Debugger> choice = rnd(xml.img.count()) - 1
BrightScript Debugger> ? choice, xml.img[choice]
2 <Component: roXMLElement>
BrightScript Debugger> ? xml.img[choice].genXML(0)
<img alt="Foligno Madonna, by Raphael" src="madonna3.jpg" />
0 Kudos