XML access voor Firefox

L

littlefool

Hi,

I'm trying to access a XML file in Firefox. It works perfect in IE
(with the ActiveXObject), but in Firefox displays it an empty page. I
found on the web examples how to do that, but I still have problems
with it. Can anyone help me with this?

This is the XML-file
<?xml version="1.0" encoding="UTF-8"?>
<constituencyseats>
<seat>
<Region>Central Scotland</Region>
<Constituency>Aidrie and Shotts</Constituency>
<Party>Labour</Party>
</seat>
<seat>
<Region>Central Scotland</Region>
<Constituency>Coatbridge and Chryston</Constituency>
<Party>Labour</Party>
</seat>
</constituencyseats>

This is the the code

this.xmlObj = loadXMLSeats('scripts/xml/Constituencyseats.xml',
'seat');

function loadXMLSeats(xmlFile, tagname){
var xmlDoc;
if(document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
}
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.onreadystatechange=verify;
}
else {
alert('your browser can\'t handle this script');
return;
}
xmlDoc.load(xmlFile);
return xmlDoc.getElementsByTagName(tagname);
}
function verify(){
// 0 Object is not initialized
// 1 Loading object is loading data
// 2 Loaded object has loaded data
// 3 data from object can be worked with
// 4 Object completely initialized
if (xmlDoc.readyState != 4) return false;
}

It appears that in Firefox the problem occurs with loading the XML
file. After loading, the object stays empty.

Thank you for your help
 
M

Martin Honnen

littlefool wrote:

I'm trying to access a XML file in Firefox. It works perfect in IE
(with the ActiveXObject), but in Firefox displays it an empty page. I
found on the web examples how to do that, but I still have problems
with it.

Look here for suggestions on how to to that:
var xmlDoc;
if(document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
}

There are more browsers than Mozilla that support the above but a load
method is only exposed by some Mozilla versions thus that call
xmlDoc.load(xmlFile);

will give an error in Opera for instance.

As for the Mozilla problem loading by default happens asynchronously so
you would need to set up an onload handler before calling the load
method. Or in newer Mozilla versions you can do synchronous loading by
setting
xmlDoc.async = false
before calling the load method. But synchronous loading blocks the
browser so it might be fine for a first test to play with XML, in your
final code on the web you should use asynchronous loading.

But check the article I linked to above, it is not necessary and helpful
to go the createDocument/load way for Mozilla as it does not work in
other browser at all while XMLHttpRequest to load XML works in lots of
browsers by now and gives you more possibilities and better error
handling (e.g. access to HTTP headers).
 

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

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top