Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pick random nodes from xml doc

Posted on 2010-11-20
2
Medium Priority
?
461 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

885 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