Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] Why won't firefox work with this xml reading javascript code?

Started by johnnyBravo Feb 20, 2005 at 10:22 PM 1 replies 900+ views
Original Post
johnnyBravo
johnnyBravo
Hi, I'm reading a xml file using &#106avascript, eg.

<html>
<head>
<xml id="dataXML" name="dataXML" SRC="data.xml"></xml>
<script type="text/javascript">
var myXML=document.getElementById("dataXML").XMLDocument; //this works
var textfieldsNode = myXML.documentElement; //so far this line won't work.
	
var itemNode = textfieldsNode.firstChild;
while(itemNode != null) {
	var nameNode = itemNode.firstChild;
	var sizeNode = nameNode.nextSibling;
	alert(nameNode.firstChild.nodeValue + ", " + sizeNode.firstChild.nodeValue);
	itemNode = itemNode.nextSibling		
}
alert("test");
</script>
</head>
<body>

hello

</body> </html>
When running this page under firefox it doesn't work, so far i've tested it to the second line, which doesn't work. I can't work out what it going wrong, as it works fine in internet explorer. thanks oh and heres the xml file

<?xml version="1.0" ?>
<textfields>
	<item>
		<name>deptunit</name>
		<size>16</size>
	</item>
	<item>
		<name>familyname</name>
		<size>14</size>
	</item>
	<item>
		<name>firstname</name>
		<size>18</size>
	</item>	
</textfields>

markr
markr
What you are trying to do is an utterly IE-specific hack which Microsoft added in some version of IE.

Of course calling getElementById works in Firefox. Of course the .documentElement property works, but it will give you the HTML document, not the external XML one.

I believe you can do what you want in Firefox, you'll need to use either XMLHttpRequest, or create a new XML DOM object and load it - I think this is achieved through document.implementation.createDocument() - possibly not.

The latter might prove useful.

Be careful about asynchronous behaviour though - it's likely that if you request a document loaded, it won't be available immediately, you might have to catch an event which gets delivered when it is available (XMLHttpRequest will do this).

EDIT:

The following works. As a bonus, there is an outside chance it'll work in IE.
var doc = document.implementation.createDocument("","",null);document.write("So far so good");doc.async = false; // make it synchronous.doc.load("test.xml");document.write("
Root node is called:" + doc.documentElement.nodeName);


Mark

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.