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: 
ioan
Level 7

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
renojim
Level 8

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
0 Kudos
EnTerr
Level 8

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