471,082 Members | 904 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

XML file access not working in Firefox

I have a script (see below) that accesses a XML file and displays the
contents through a series of document.write calls. This all works fine
in IE, but not at all in Firefox. I get no errors in the javascript
console, but when I try to load the elements by tag name and look at
the length it is always 0 as if no elements have been added. Any
suggestions are greatly appreciated as I've been stuck on this for a
while now.

Thanks,

Laramie Hartmann

Here is a sample record from my XML file...

<?xml version="1.0" ?>

<DOCTOR>
<PHOTO>facholonu.jpg</PHOTO>
<NAME>Felix N. Acholonu, M.D.</NAME>
<SNAME>FACH</SNAME>
<OFFICE>
<ADDRESS>
<STREET>1801 Barrs St., Ste. 920</STREET>
<CITY>Jacksonville</CITY>
<STATE>FL</STATE>
<ZIP>32204</ZIP>
<PHONE>(904) 387-1401</PHONE>
</ADDRESS>
<ABBR>STV5</ABBR>
<FULL>St. Vincent's 5</FULL>
</OFFICE>
<SPECIALTY>Obstetrics &amp; Gynecology</SPECIALTY>
<HOSPITAL>St. vincent's Medical Center</HOSPITAL>
<COLLEGE>SUNY Health Science Center</COLLEGE>
<GRADUATION>1981</GRADUATION>
<BOARD>American Board of Obstetrics &amp; Gynecology</BOARD>
<BLURB></BLURB>
</DOCTOR>

Here is the javascript...

<script type="text/javascript">
//<![CDATA[
if (window.ActiveXObject)
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false; //Enforce download of XML file first. IE only.
document.write("Internet Explorer");
}
else if (document.implementation &&
document.implementation.createDocument)
{

xmlDoc = document.implementation.createDocument("", "doc", null);

document.write("Firefox");
}

xmlDoc.load("caregivers.xml");

var x=xmlDoc.getElementsByTagName("PHOTO")
var nm=xmlDoc.getElementsByTagName("NAME")
var sname=xmlDoc.getElementsByTagName("SNAME")
var abbr=xmlDoc.getElementsByTagName("ABBR")
var full=xmlDoc.getElementsByTagName("FULL")
var street=xmlDoc.getElementsByTagName("STREET")
var city=xmlDoc.getElementsByTagName("CITY")
var state=xmlDoc.getElementsByTagName("STATE")
var zip=xmlDoc.getElementsByTagName("ZIP")
var phone=xmlDoc.getElementsByTagName("PHONE")
var specialty=xmlDoc.getElementsByTagName("SPECIALTY")
var tem = window.location.search;
tem = tem.substring(10,14);

for (var i = 1; i <= x.length; i++)
{

if (tem == abbr.item(i-1).text || tem == "" || tem == "ALLD")
{
document.write("<div class='square'>")
document.write("<img class='bimg' src='" + x.item(i-1).text + "'
width='125' height='150'></img>")
document.write("<div class='nameblock'>")

document.write("<strong><a href='bio.html?caregiver=" +
sname.item(i-1).text + "'>" + nm.item(i-1).text + "</a></strong><br
/>")
document.write("<a href='division.html?division=" +
abbr.item(i-1).text + "'>" + full.item(i-1).text + "</a><br />")
document.write(street.item(i-1).text + "<br />")
document.write(city.item(i-1).text + ", " + state.item(i-1).text + " "
+ zip.item(i-1).text)

document.write("</div>")
document.write("<div class='clearer'></div>")
document.write("</div>")
}
}
var division = document.getElementById("division")

for (i = 0; i < division.length; i++)
{
if (tem == division.options[i].value)
{
division.options[i].selected = true
}
}

var name=xmlDoc.getElementsByTagName("NAME")
var docs = document.getElementById("caregiver")
var len = docs.length

for (var i = 0; i < sname.length; i++)
{
docs.options[len+i] = new Option(name.item(i).text)
docs.options[len+i].value = sname.item(i).text
}

//]]>
</script>

Oct 11 '06 #1
6 3865


la**************@gmail.com wrote:
I have a script (see below) that accesses a XML file and displays the
contents through a series of document.write calls. This all works fine
in IE, but not at all in Firefox. I get no errors in the javascript
console, but when I try to load the elements by tag name and look at
the length it is always 0 as if no elements have been added.
<script type="text/javascript">
//<![CDATA[
if (window.ActiveXObject)
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false; //Enforce download of XML file first. IE only.
document.write("Internet Explorer");
}
else if (document.implementation &&
document.implementation.createDocument)
{

xmlDoc = document.implementation.createDocument("", "doc", null);

document.write("Firefox");
}

xmlDoc.load("caregivers.xml");
By default Mozilla loads the XML asynchronously so you need to add an
onload handler and process the XML there (e.g. call getElementsByTagName
in the onload handler). Or you need to use xmlDoc.async = false before
you call the load method.
In my view if you want to load XML cross browser then using
XMLHttpRequest/XMLHTTP is easier as it requires less code differences, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>

Opera 8 for instance supports XMLHttpRequest but no load method for XML
DOM documents.

So consider loading the XML asynchronously with XMLHttpRequest/XMLHTTP
and then reading out values from the XML with the DOM and adding
contents to the HTML document with the DOM.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 11 '06 #2
Martin - Thanks for the response. I'm being led in the right direction,
but I still haven't been able to make everything work properly.

In Mozilla does x.item(i-1).text work given that:

var x=xmlDoc.getElementsByTagName("PHOTO")

I can't get it working correctly, but it does display fine in IE. To
work around this problem I tested

document.write(x[0].firstChild.nodeValue);

instead. In Mozilla it writes the value, but the rest of the page never
loads. No errors in the Javascript console though.

Here is my new code for testing purposes. I must admit, the new test
code below does not work in IE. It simply loads the HTML (unlike
Mozilla), but does not throw any errors.

<script type="text/javascript">
//<![CDATA[

if (window.ActiveXObject)
{
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false; //Enforce download of XML file first. IE only.
xmlDoc.onload = readXML;
document.write("Internet Explorer");
}
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "doc", null);
xmlDoc.onload = readXML;
document.write("Firefox.");
}

function readXML()
{
x=xmlDoc.getElementsByTagName("PHOTO")
nm=xmlDoc.getElementsByTagName("NAME")
sname=xmlDoc.getElementsByTagName("SNAME")
abbr=xmlDoc.getElementsByTagName("ABBR")
full=xmlDoc.getElementsByTagName("FULL")
street=xmlDoc.getElementsByTagName("STREET")
city=xmlDoc.getElementsByTagName("CITY")
state=xmlDoc.getElementsByTagName("STATE")
zip=xmlDoc.getElementsByTagName("ZIP")
phone=xmlDoc.getElementsByTagName("PHONE")
specialty=xmlDoc.getElementsByTagName("SPECIALTY")

//Firefox displays this info (IE doesn't) , but writes to a blank page
and the hour glass pointer
// never goes away.

document.write("Variable x has " + x.length + " values. and the 5th
value is ")
document.write(x[0].firstChild.nodeValue);
}

xmlDoc.load("caregivers.xml")
//]]>
</script>

Oct 12 '06 #3


la**************@gmail.com wrote:

In Mozilla does x.item(i-1).text work given that:
Mozilla's DOM implementation (mainly) follows the W3C Level 2 DOM
specification. There is no property named |text| in that object model,
|text| is an extension MSXML introduced (and I think Opera 9 has now too).
With Firefox however there is support for the W3C DOM Level 3 property
named |textContent| so if you have a node and want the concatenation of
all text in the node and child/descendant nodes then you can use e.g.
var text;
if (typeof node.textContent != 'undefined') {
text = node.textContent;
}
else if (typeof node.text != 'undefined') {
text = node.text;
}
else if (typeof node.innerText != 'undefined') {
text = node.innerText;
}

As for the other problems I already suggested to load asynchronously and
then once the XML is loaded not to use document.write to show the XML
contents but to use DOM scripting with createElement and appendChild.
That means you write a complete HTML document with script loading the
XML and script creating HTML elements and inserting them once the XML
has been loaded.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 12 '06 #4
Thank you for your replies. I can't get my onLoad function to work. I
have stripped out any XML parsing and I am only trying to load the XML
document and call my onLoad function. In Firefox it prints my one
document.write line, but displays nothing else except a white
background. As before the hour glass pointer never goes away. Here is
my code.

script type="text/javascript">
//<![CDATA[

var loader

function loaded()
{
loader=5
document.write(loader)
}

if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null)
xmlDoc.load("caregivers.xml")
document.write(" Firefox. ")
xmlDoc.onload = loaded
}
//]]>
</script>

Oct 12 '06 #5
Apparently the document is written over with the document.write
command. So I placed a DO loop to make sure that the XML is completely
loaded before the document.write command works. So at this point I
think I've got it figured out for the most part.

Oct 12 '06 #6
So I thought I had it figured out, but Firefox is not pulling the
getElementsByTagName. I am at a loss. In the Javascript console I get
"x[5] has no properties" even though I clearly set x to hold all tags
with the name "PHOTO". I am not a programmer and this has been driving
me mad for 2 days. I am not familiar with programming with the DOM
which is why I am using document.write. Everything worked great in IE,
but I just can't get Firefox to do the trick.

<script type="text/javascript">
//<![CDATA[

var xmlDoc = document.implementation.createDocument("", "", null);
document.write("Firefox.")
isLoaded = xmlDoc.load("caregivers.xml")
while (isLoaded != true){}
readXML()

function readXML()
{
x=xmlDoc.getElementsByTagName("PHOTO")

document.write("Variable x has " + x.length + " values. and the 5th
value is ")
document.write(x[5].firstChild.nodeValue);
}

//]]>
</script>

Oct 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mark Olbert | last post: by
2 posts views Thread by Joey | last post: by
7 posts views Thread by Jeff Gaines | last post: by
3 posts views Thread by =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post: by
reply views Thread by leo001 | last post: by

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.