3 Replies - 2482 Views - Last Post: 23 October 2009 - 10:07 AM

#1 Public Designs   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Change JS Random quote to XML caller

Post icon  Posted 21 October 2009 - 10:51 AM

I am trying to take code that I have and change it to be called from an XML file.

Right now I have random quote and random author arrays stored in an external .js file. And then a function to make everything random. Then I have it called and modified using the innerhtml method.

All of this works fine and I will be including that code.

Now I want to modify this and change it to where I can store my quotes and my quote authors in an xml file.
Then called through the JS.
(more info under this code)

var arrQuote=new Array() 
var arrAuth=new Array(); 

arrQuote[0] = "Four score and...";
arrQuote[1] = "Money cant buy me love";
arrQuote[2] = "welcome back";
arrQuote[3] = "hello world";
arrQuote[4] = "youve got mail";
arrQuote[5] = "im a pc";

arrAuth[0] = "Lincoln";
arrAuth[1] = "Beetles";
arrAuth[2] = "noah";
arrAuth[3] = "computer";
arrAuth[4] = "aol";
arrAuth[5] = "windows commercials";




function ChangeText()
{
	var numQuote = arrQuote.length;
	var whichQuotation=Math.floor(Math.random()*numQuote);
	var quote = document.getElementById("quoteText");
	quote.innerHTML = (arrQuote[whichQuotation]);
	var author = document.getElementById("authorText");
	author.innerHTML = (arrAuth[whichQuotation]);
}

window.onload=ChangeText;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> --><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content="HTML Tidy for Linux (vers 6 November 2007), see www.w3.org" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Glaser's Javascript Page</title>
<link rel="stylesheet" type="text/css" href="csjsMain.css" />
<script type="text/javascript" src="lab2.js"></script>

</head>
<body>
<div id="container">
<div id="header">
<h1>Noah Glaser's Lab 2 Submission</h1>
</div>
<div id="content">


<span id="quoteText"></span><br />
<span id="authorText"></span><br />
  <input type="button" value="Change Quote" onclick="ChangeText()">

</div>

<div id="footer">&copy; 2009 Noah Glaser </div></div>

</body>
</html>




I have been supplied with this code here to include as part of the way to call the quotes.

 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;
	xmlDoc.load("quotes.xml");



So I am assuming that I will be making a quotes.xml file and then having to set up the xml for my quotes and all.

Now here is the problem. I know nothing about xml but I hear it is fairly simple justs omething like

<quote>enter text here</quote><author>more</author>

Is this right? Could somebody get me started with the xml file and I would be able to take it from there?

And then I will have to change my function to update based on a xml file instead of an array.

Ideas?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Change JS Random quote to XML caller

#2 ahmad_511   User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Change JS Random quote to XML caller

Posted 21 October 2009 - 11:17 AM

Hi,
first you have to have a root element that will contains all your xml data.
then in order to read the quotes in proper way, put each quote and it's related info (the author,.....) in a separate tag (item for example).
for your example you can use something like this:
<root>
	<item>
		<quote>enter text here1</quote>
		<author>more1</author>
	</item>
	
	<item>
		<quote>enter text her2e</quote>
		<author>more2</author>
	</item>
	.
	.
	.
	.
	
</root>



or you can use attributes to store the quote's related info
<root>
	<quote author="more1">enter text here1</quote>
	<quote author="more2">enter text here2</quote>
	.
	.
	.
</root>


it depends on the way you're thinking of to read the xml file

i found this very helpful http://www.w3schools...xml/default.asp
Was This Post Helpful? 0
  • +
  • -

#3 Public Designs   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: Change JS Random quote to XML caller

Posted 21 October 2009 - 11:49 AM

Thank you for that link. That will take care of the xml file page.

Now I need to figure out how to change myfunction from arrays to xml elements.
Was This Post Helpful? 0
  • +
  • -

#4 Public Designs   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: Change JS Random quote to XML caller

Posted 23 October 2009 - 10:07 AM

function ChangeText()
{
	var numQuote = arrQuote.length;
	var whichQuotation=Math.floor(Math.random()*numQuote);
	var quote = document.getElementById("quoteText");
	quote.innerHTML = (arrQuote[whichQuotation]);
	var author = document.getElementById("authorText");
	author.innerHTML = (arrAuth[whichQuotation]);
}

window.onload=ChangeText;



How would I change this function to call an xml file with the quotes instead of my arrays? I have this for my xml

<?xml version="1.0" encoding="utf-8"?>
<root>
		<quoteinfo>
				<quote>Four score and...</quote>
				<author>Lincoln</author>
		</quoteinfo>
		
		 <quoteinfo>
				<quote>Money cant buy me love</quote>
				<author>Beatles</author>
		</quoteinfo>
		
		 <quoteinfo>
				<quote>welcome back</quote>
				<author>noah</author>
		</quoteinfo>
		
		 <quoteinfo>
				<quote>hello world</quote>
				<author>computers</author>
		</quoteinfo>
		
		 <quoteinfo>
				<quote>youve got mail</quote>
				<author>aol</author>
		</quoteinfo>
		
		
		
</root>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1