Computer Science Canada

Suggestions for Javascript/XML flexibility?

Author:  asteffes [ Mon Aug 14, 2006 1:00 pm ]
Post subject:  Suggestions for Javascript/XML flexibility?

Hello all,

A website I am working on uses javascript to read 6 headlines from an XML file and then outputs them to a <p> element by simply referring to the element's ID and filling it with the XML content. I need to have the ability to easily support 12 headlines.

The requirement is for a vice president here (who knows little of html/javascript/XML) simply add headlines to the XML file and have them automatically show up in the web page.

Currently I am just stepping through the XML file and outputting which won't do the job. I need to have some type of loop and if statement, but I have limited skills with java/xml. My code below will help illustrate whats going on. Thanks so much for any help!!

Here is my html code, followed by Javascript, followed by XML:
HTML:
<p class="headlines" id="headline1"></p>
<br class="altheight">
<p class="headlines" id="headline2"></p>
<br class="altheight">
...etc through "headline 6"

Javascript:

var xmlDoc
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("KDXHeadlines.xml");
getmessage()
}
// code for Mozilla, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("KDXHeadlines.xml");
xmlDoc.onload=getmessage
}
else
{
alert('Your browser cannot handle this script');
}
}

function getmessage()
{
document.getElementById("headline1").innerHTML=xmlDoc.getElementsByTagName("headline1")[0].firstChild.nodeValue
document.getElementById("headline2").innerHTML=xmlDoc.getElementsByTagName("headline2")[0].firstChild.nodeValue
...etc through "headline 6"
}

XML

<?xml version="1.0" encoding="ISO-8859-1" ?>
<headlines>
<headline1>Our team wins</headline1>

<headline2>Yadda yadda</headline2>
...etc through <headline 6>
</headlines>

To summarize, I need the ability to simply add to the xml file and have it output to the html file. I have attempted to create blank XML tags (i.e.
<headline7></headline7> but they provoke an error.

Any help is much appreciated! I don't know where to go from here.

Author:  OneOffDriveByPoster [ Wed Aug 16, 2006 12:02 am ]
Post subject: 

Maybe you should try the "XHTML help" board.

You could also try looking into XSLT and server side scripting.


: