473,473 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

trouble using mozilla xml loading code

Hi there,

I am using the following function to import a xml file whether the
users browser be IE or Mozilla:

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') && (typeof
document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');

if (moz) {
xmlDoc = document.implementation.createDocument("", "", null)
//do I need something else here???
} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
}
xmlDoc.load(file);
return xmlDoc
}

An example call of this function looks like this:

importXML('xml/books.xml')

The code for IE works well as I have used code to access the XML file
im using but the code for Mozilla doesnt work. I got the funxtion from
a tutorial site, http://www.sitepoint.com/article/xml-javascript-mozilla.

Am I missing something after the line 'xmlDoc =
document.implementation.cre...?? On the tutorial it had a line
xmlDoc.onload = readXML but that appears to be some kind of function,
readXML, and isnt shown on the tutorial. How do I get the variable
xmlDoc to the same stage as the code used for IE so that I can then
perform something like after the function is run??:

nodes = xmlDoc.getElementsByTagName("title")

or

nodes = xmlDoc.documentElement.childNodes
If I try the above in Mozilla and do something like nodes.length I get
0 (wrong) whereas if I do it in IE, I get 10 (correct). Is the 'nodes
= ...' code only able to run in IE or is the variable, xmlDoc, not
properly prepared in Mozilla to do the above?? The results I get in
Mozilla lead me to assume either but I may be wrong. Any help would be
great.

Cheers

Burnsy
Jul 23 '05 #1
1 1936


mr_burns wrote:

I am using the following function to import a xml file whether the
users browser be IE or Mozilla:

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') && (typeof
document.implementation.createDocument != 'undefined');


Other browsers have document.implementation.createDocument as well so at
least the name of the variable 'moz' is doubtful.

While Mozilla allows you to create an XML document with the method you
check for above and then exposes a load method that stuff is Mozilla
only while by now Safari and recent Opera version implement
XMLHttpRequest thus I would strongly suggest to use that if you want to
load XML:

function loadXML (url, processXML) {
var httpRequest;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
if (httpRequest) {
httpRequest.open('GET', url, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.responseXML) {
processXML(httpRequest.responseXML);
}
};
httpRequest.send(null);
}
}

function exampleXMLHandler (xmlDocument) {
if (xmlDocument.documentElement) {
alert(xmlDocument.documentElement.childNodes.lengt h);
}
}
loadXML('test2005010601.xml', exampleXMLHandler)

That should do with IE/Win (versions 5, 5.5, 6 and hopefully later),
Mozilla 1.0 and later, Safari 1.2 (not tested now), Opera 7.6x , 8.00 or
later, as long as the XML loaded is well-formed. If the parsing of the
XML fails then unfortunately every implementation has its own way to
indicate parse errors so catering for that is a bit too much to handle
here in a newsgroup post.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter | last post by:
Hello, First of all, sorry for all the code. I don't know how to explain my problem without it. I have a javascript function which can build a webpage dynamically. A striped down version of...
2
by: Ola Fjelddahl | last post by:
hi. I load a script dynamically and it works * everytime with IE6. * sometimes! with Mozilla1.5 <- makes me curious * never with Opera7.11 all tests on WindowsXP. With "sometimes" I mean
3
by: kj | last post by:
This problem is driving me nuts. The code at the end of this post below works fine with IE, but fails with Mozilla. You can see it in action at http://tinyurl.com/2jvo3 With Mozilla 1.4 and...
4
by: ekimnosnews | last post by:
I'm programming a JS tile map editor for my friend's game. The map editor has different "sections" that contain different tiles. The way that it works is when you select a section it calls a JS...
17
by: George Hester | last post by:
http://tinyurl.com/5uj6w The lower middle icon the "block" should not drop down when the mouse is over it. How can I stop that? Also the navigation divs both top and bottom should follow the...
9
by: johnd126 | last post by:
I have a cgi program which outputs a fairly hefty amount of html/javascript for doing a complex slide show sorta thing in a variety of areas in the browser. I accomplish this by creating a series...
1
by: newToAjax | last post by:
I have created an ajax application which retrievs an xml file and fills in the tab fields on the form.The code works fine in IE while its does not in Mozilla. Can you please let me know if i have to...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.