Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Level 9

Choose a random XML element

Let's say I have this xml:

<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' />

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

0 Kudos
2 Replies
Level 21

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

0 Kudos
Level 11

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