Jump to content

Reading XML file to webpage


brucewc3

Recommended Posts

I am interested in using javscript to read from an XML file into a webpage, a new experience for 'simplewebman' . Studying w3c schools gives me many examples, but as far as I can see all recommend code such as.....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","cd_catalog.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML;........to read in the xml My problem is that my policy to date has been to develop code in house and be very sure before committing it to my host's server and I don't see how to use XMLHttpRequest and also follow my policy. I want to first read the XML file from a directory on my home computer and thus simulate the results which will occur using the host's server.

Link to comment
Share on other sites

Install a webserver onto your computer and test your page on the localhost. Or just test your code in any browser that's not Internet Explorer.

Link to comment
Share on other sites

Install a webserver onto your computer and test your page on the localhost. Or just test your code in any browser that's not Internet Explorer.
Thank you very much Ingolme for your advice. I have not acted on all of it yet but I thought I should report to thank you and say what I have done so far. I looked into buying server software but it was beyond the budget of an old age pensioner. In any case if needs must I think I could slip the xml file into my webspace for one of my websites (not linked) and test it from there. I am puzzled by the distinction you make between Internet Explorer and the other browsers. In both cases the alternative codings supplied by W3C Schools use the term' http', which I have always understood refers to traffic across the internet and not solely within a computer. I would like to be able to address a directory on my computer without reference to a server. I am working on trying to understand. (I am 'simple' webman!)
Link to comment
Share on other sites

You don't have to buy anything. There is free server software that's easy to install, like WAMP.Normal browsers will perform an AJAX request on the local filesystem without any problem, but Internet Explorer does not allow local HTML files to perform AJAX requests.

Link to comment
Share on other sites

  • 3 weeks later...
You don't have to buy anything. There is free server software that's easy to install, like WAMP.Normal browsers will perform an AJAX request on the local filesystem without any problem, but Internet Explorer does not allow local HTML files to perform AJAX requests.
I would like to give a progress report on the work I did after getting INGOLME's kind advice. I tried several examples from W3C Schools, using the non AJAX method setting xmlhttp.open("GET","file.xml", false); ......and they worked fine, thank you Ingolme, using Firefox as the browser (not Explorer) and with my file.xml in the same directory section as my test html/script. I thought I was underway.Fine, but I got cold feet (anxious state) worrying if my visitor was going to have to download my entire xml database to get just one or two items (and it will be big). Also I read that the AJAX method is best. Therefore I tried the AJAX setting xmlhttp.open("GET","file.xml", true) [incidently does the entire XML file load into the visitor's computer with either method?]In fact I used the W3C shools example with a text file as follows.........<html><head><script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }xmlhttp.open("GET","file.txt",true);xmlhttp.send();}</script></head><body><div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" onclick="loadXMLDoc()">Change Content</button></body></html>That didn't work in Firefox in that it didn't write the text into the <div> although the text file was examined because the debugger wrote it out and attributed a syntax error as followsError: syntax errorSource File: file:///C:............................................/file.txt Line: 1, Column: 1Source (of text file!) Code:th (error cursor here) e best I can do
Link to comment
Share on other sites

Ignore Firefox's errors from the included file. I think it throws those kind of errors if the file isn't XML, but you can still use the returned data however you like, the error doesn't prevent you from it.The problem is that when running on the filesystem the xmlhttp.status returns 0 instead of 200. This is because the request is not being done over HTTP so no HTTP response header is sent.Unfortunately, the whole XML file is downloaded each time. If you want to be selective of the data returned then your only option would be to use a server-side language.

Link to comment
Share on other sites

Install a webserver onto your computer and test your page on the localhost. Or just test your code in any browser that's not Internet Explorer.
hahaha, NO KIDDING !!i was wondering what exactly made it so "special".
You don't have to buy anything. There is free server software that's easy to install, like WAMP.Normal browsers will perform an AJAX request on the local filesystem without any problem, but Internet Explorer does not allow local HTML files to perform AJAX requests.
ahh... now i know.such a weird animal that "Microsoft"...
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...