NN and XML

G

Greg Griffiths

Dear All,
I've got a small JS app that uses XML and I've got it working fine
in IE, but not in NN, I've used plenty of the samples on the web with my
Netscape Communicator and NN 6.1 but they just to seem to solve the
problem. The relevant code is below :


var text = new Array();
var header = new Array();
var linka = new Array();
var targa = new Array();
var newsItems;

// create an XMLDoc
var xmldoc;

if (document.implementation && document.implementation.createDocument)
{
xmldoc = document.implementation.createDocument("", "", null);
}
else if (window.ActiveXObject)
{
// create a new XML object for IE
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
}
else
{
alert('Your browser can\'t handle this script');
}

// define some attributes of the doc
xmldoc.async = false;

//load the XML file in
xmldoc.load('news.xml');

// load the moves
newsItems = xmldoc.getElementsByTagName("newselement");

// populate the Move Arrays from the XML file
for (var i=0;i<newsItems.length;i++)
{
// add the move

text=newsItems.getElementsByTagName("headline")[0].firstChild.data;


header=newsItems.getElementsByTagName("newsitem")[0].firstChild.data;


linka=newsItems.getElementsByTagName("newslink")[0].firstChild.data;


targa=newsItems.getElementsByTagName("newstarget")[0].firstChild.data;

}


with the XML file being used as follows :


<newsticker>
<newselement>
<newsno>1</newsno>
<headline>This is Headline 1</headline>
<newsitem>This is NI 1</newsitem>
<newslink>helloWorld.html</newslink>
<newstarget>a</newstarget>
</newselement>
<newselement>
<newsno>2</newsno>
<headline>This is Headline 2</headline>
<newsitem>This is NI 2</newsitem>
<newslink>http://www.greggriffiths.org/</newslink>
<newstarget>new</newstarget>
</newselement>
<newselement>
<newsno>3</newsno>
<headline>This is Headline 3</headline>
<newsitem>This is NI 3</newsitem>
<newslink>http://www.greggriffiths.org/</newslink>
<newstarget>blank</newstarget>
</newselement>
</newsticker>


Can anyone help me out with this as it is driving me nuts !!
 
M

Martin Honnen

Greg said:
I've got a small JS app that uses XML and I've got it working fine
in IE, but not in NN, I've used plenty of the samples on the web with my
Netscape Communicator and NN 6.1 but they just to seem to solve the
problem. The relevant code is below :

// create an XMLDoc
var xmldoc;

if (document.implementation && document.implementation.createDocument)
{
xmldoc = document.implementation.createDocument("", "", null);
}
else if (window.ActiveXObject)
{
// create a new XML object for IE
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
}
else
{
alert('Your browser can\'t handle this script');
}

// define some attributes of the doc
xmldoc.async = false;

This is the problem, Netscape 6.1 doesn't support async so what you need
for Netscape/Mozilla is an onload handler
xmldoc.onload = function (evt) {
// access document here
}

I think recent versions of Mozilla (since 1.4) support async but you
shouldn't rely on having a Mozilla 1.4 or 1.5 user on a Web page. Indeed
asynchronous loading is much more user friendly.

An alternative way to load XML documents is the
XMLHttpRequest
object (similar to MSXML XMLHTTP), this allows synchronous loading even
with earlier Mozilla/Netscape versions.
 
G

Greg Griffiths

Martin,
can you amend my code, I'm having a little difficulty seeing where your
changes would go ?
 
M

Martin Honnen

Greg said:
Martin Honnen wrote:


can you amend my code, I'm having a little difficulty seeing where your
changes would go ?

For the Netscape/Mozilla solution you simply need to put the code that
processes the XML document into a function which you set as an onload
event handler before you call the load method:

function processXMLDocument (evt) {
alert(xmlDocument.documentElement.nodeName);
// do here what you want to do with the document
}
var xmlDocument = document.implementation.createDocument('', '', null);
xmlDocument.onload = processXMLDocument;
xmlDocument.load('test20031030.xml')


As for an IE and Netscape example for that I think it is better to set
up some functions that do the work, I will see if I have the time to
come up with an example later.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,093
Messages
2,570,607
Members
47,227
Latest member
bluerose1

Latest Threads

Top