View Full Version : XML to array - problems printing results

George Lucas
12-04-2009, 06:08 AM
I'm trying to display a random message by making an array through values in an XML file and then displaying the info on the page. i want to have it so that a new message is generated each time the button is clicked with no repeats. here is what i have

function initial()

var xmlDoc;

// First option for internet explorer 5 and 6, second option for firefox, internet explorer 7+
window.ActiveXObject ? xmlDoc = new ActiveXObject("Microsoft.XMLDOM") : xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async = false;

// take the quote and author information from the xml file
var pQuotes = xmlDoc.getElementsByTagName('quote');
var pAuthors = xmlDoc.getElementsByTagName('author');

// create arrays
var aQuotes = new Array();
var aAuthors = new Array();

// put the quotes into the array
for (i=0; i<pQuotes.length; i++)

// put the authors into the array
for (i=0; i<pAuthors.length; i++)

// Loop through arrays and print their quotes and authors
for (var s in aQuote) {

var Q = aQuotes.length;

var quoteText = document.getElementById('aQuotes');
var authorText = document.getElementById('aAuthors');
var randomquote = Math.floor(Q * Math.random());

function quoter() {

var randomquote2 = randomquote;

while (randomquote == randomquote2)
randomquote = Math.floor(Q * Math.random());

document.getElementById('quoteText').innerHTML = aQuotes[randomquote];
document.getElementById('authorText').innerHTML = " - " +aAuthors[randomquote];
quote.innerhtml = aQuotes[randomquote];
author.innerhtml = aAuthor[randomquote];


navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);



<?xml version="1.0" encoding="utf-8" ?>

<quote>Over the years your bodies become walking autobiographies, telling friends and strangers alike of the minor and major stresses of your lives.</quote>
<author>Marilyn Ferguson</author>

<quote>In reality, serendipity accounts for one percent of the blessings we receive in life, work and love. The other 99 percent is due to our efforts.</quote>
<author>Peter McWilliams</author>

<quote>The secret of joy in work is contained in one word - excellence. To know how to do something well is to enjoy it.</quote>
<author>Pearl Buck</author>

<quote>The discovery of a new dish does more for human happiness than the discovery of a new star.</quote>
<author>Anthelme Brillat-Savarin</author>

<quote>When I was born I was so surprised I didn't talk for a year and a half.</quote>
<author>Gracie Allen</author>



<body onload="initial()">

<div id="container">

<div id="header">

<div id="content">

<span id="quoteText"></span>

<span id="authorText"></span>
<input type="button" value="Click here for another quote" onclick="quoter()"/>


Philip M
12-04-2009, 08:58 AM
More homework?

This should move you forward:-

<input type = "button" value = "Show next quote" onclick = "showNext()"><br>
<div id = "theQuote"></div>

<script type = "text/javascript">

var quoteArray = [];
quoteArray[0] = "Time is of the essence! Comb your hair.";
quoteArray[1] = "Sanity is a golden apple with no shoelaces.";
quoteArray[2] = "Repent! The end is coming, $9.95 at Amazon.";
quoteArray[3] = "Honesty blurts where deception sneezes.";
quoteArray[4] = "Pastry satisfies where art is unavailable.";
quoteArray[5] = "Delete not, lest you, too, be deleted.";
quoteArray[6] = "O! Youth! What a pain in the backside.";
quoteArray[7] = "Wishes are like goldfish with propellors.";
quoteArray[8] = "Love the river's \"beauty\", but live on a hill."; // note escape quotes
quoteArray[9] = "Invention is the mother of too many useless toys.";

function randOrd(){return (Math.round(Math.random())-0.5); }
quoteArray.sort(randOrd); // shuffle the array

var count = 0;
function showNext() {
document.getElementById("theQuote").innerHTML = quoteArray[count];
count ++;
if (count >9) {
quoteArray.sort(randOrd); // shuffle the array again
count = 0;


"The only thing to do with good advice is pass it on. It is never any use to oneself." - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)