Solved

pick random nodes from xml doc

Posted on 2010-11-20
2
449 Views
Last Modified: 2012-05-10
I have a page with 3 spaces for information pulling from an xml file. My xml file has 20 nodes of different  information.
What can I put on my page in javascript or asp to pull a random different 3 each time the page loads?
0
Comment
Question by:rivkamak
2 Comments
 
LVL 1

Expert Comment

by:him6ul
ID: 34182169
You can write an algorithm using Random number implementation to give you three unique random numbers between 1 and 20 everytime you call that function.

Then you can pass those numbers to your processor which is processing XML node and it can give you the XML nodes data corresponding to those three numbers.
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34182171

Without knowing your xml structure, the following picks three values from an array:

<script>
// http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26629425.html

var nodes = ["one","two","three","four","five"
  ,"six","seven","eight","nine","ten"
  ,"eleven","twelve","thirteen","fourteen","fifteen"
  ,"sixteen","seventeen","eighteen","nineteen","twenty"];
var pick = Array();

while ( pick.length < 3 )
{
  var index = randomTo( 20 );
  while ( isIn( pick, index ) )
  {
    index = randomTo( 20 );
  }
  pick[pick.length] = nodes[index];
}
alert(pick);

function randomTo( modulo )
{
  return Math.floor( Math.random() * new Date().getMilliseconds() ) % modulo;
}

function isIn( array, value )
{
  result = false;
  for ( var i = 0; i < array.length; i++ )
  {
    if ( array[i] == value )
    {
       result = true;
       break;
    }
  }
  return result;
}
</script>

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Read about why website design really matters in today's demanding market.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now