Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2011, 08:34 PM   PM User | #1
shaggykyle
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
shaggykyle is an unknown quantity at this point
Load an XML File - Cross-browser

So I was reading up on how to load and parse an XML file into an html page and I found through w3 a great tutorial with sample code:

http://www.w3schools.com/xml/xml_dom.asp

It appears to work fine on their website but when I try it from my desktop it doesn't work. So what I have is an html file with contents:
Code:
<html>
<body>
<h1>W3Schools Internal Note</h1>
<div>
<b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</div>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>

</body>
</html>
And then the xml file named "note.xml":

Code:
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
I have no idea what I am missing or not seeing that is causing this not to work but if anyone knows I would appreciate the input. For the record I have tried this in chrome and ie.
shaggykyle is offline   Reply With Quote
Old 10-12-2011, 09:56 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
There are two reasons why
the code you posted will not
work with IE on the file system.
It does work with Firefox,
the following code works with
firefox and IE, as long as you
have note.xml on your desktop
with the htm file .

Code:
<html>
<body>
<h1>W3Schools Internal Note</h1>
<div>
<b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</div>

<script type="text/javascript">

var xmlhttp=false;
var ie = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
 ie=true;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
  
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
if(ie){
xmlDoc = new ActiveXObject ("microsoft.xmldom");
xmlDoc.loadXML(xmlhttp.responseText); 
}


document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>

</body>
</html>And then the xml file named "note.xml":
The first reason is IE XMLHttpRequest won't
normally read the file system but ActiveXObject
will read file system.
Second IE only gives responseXML if the
file has XML headers , you get those
headers from server not file system.

Last edited by DaveyErwin; 10-12-2011 at 10:05 PM..
DaveyErwin is offline   Reply With Quote
Old 10-12-2011, 10:02 PM   PM User | #3
shaggykyle
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
shaggykyle is an unknown quantity at this point
Works for IE but not for Chrome
shaggykyle is offline   Reply With Quote
Old 10-12-2011, 10:29 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by shaggykyle View Post
Works for IE but not for Chrome
This is because of a security
setting in Chrome, go to
your run box and type in ...
chrome --allow-file-access-from-files
now when chrome opens
it will read from file system.

Last edited by DaveyErwin; 10-12-2011 at 10:52 PM..
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:58 AM.


Advertisement
Log in to turn off these ads.