Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
request Object w/ XML File On Desktop
Old 04-06-2009, 10:09 PM request Object w/ XML File On Desktop
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Hello everyone...

I'm having issues with loading an XML file that's on my desktop into the request object. 1. The XML file is validated 2. The relative path name is correct.

The issue is when I use Firebug to walk through the code, immediately after request.send(null) and if (request.readyState==4), I get the alert error mentioned at the bottom of the code. I'm afraid I've reached a dead end here unless any one of you have any suggestions.

Code:
function beginSearch(the_word,x){

var request=null;
var xml_response=null;

if (window.XMLHttpRequest){
    request=new XMLHttpRequest();
    }else if (window.ActiveXObject){
     request=new ActiveXObject("Microsoft.XMLHTTP");
}

if (request){
    request.open("GET","rb_artist2.xml");
    request.onreadystatechange = 
    function(){
        if (request.readyState==4)
        {
            if(window.ActiveXObject){
            xml_response=new ActiveXObject ("Microsoft.XMLDom");
            xml_response.loadXML(request.responseText);
            }
              else
            { 
            xml_response=request.responseXML;
            }
          compareUserEntry(xml_response,the_word,x);
        }
        else
        {
               alert("Sorry, there was a problem while getting access"+" to the file");
                return;
            }
         }
         request.send(null);
 }else{
  
  alert ("sorry, you must update your browser before seeing" + "Ajax in action");
 }
}
This is all you really need to see...the rest of the code I can walk through and debug myself. I just need the file to upload.
LayneMitch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-07-2009, 02:51 AM Re: request Object w/ XML File On Desktop
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I seem to remember having a problem like this, a couple of years ago.
And the answer was simply to serve the file via a web server. At least it was the solution at this time.
The file:// protocol seems to behave differently from the http:// protocol.
something related to the content-type of the file, I think.

Did you tried to pass through a web server ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-08-2009, 02:14 AM Re: request Object w/ XML File On Desktop
Skilled Talker

Posts: 70
Location: Atlanta, GA
Trades: 0
Quote:
Originally Posted by tripy View Post
I seem to remember having a problem like this, a couple of years ago.
And the answer was simply to serve the file via a web server. At least it was the solution at this time.
The file:// protocol seems to behave differently from the http:// protocol.
something related to the content-type of the file, I think.

Did you tried to pass through a web server ?
No, I'm trying to do it without server side PHP code. I haven't learned that yet (although I would know how to create it to call an xml file on the server). Just in case I need to go that route, I had my sites hosting company switch to a Linux server that host PHP code.

In the meantime, I'll continue to work with the http://protocol...let me know if you have any suggestions..Thanks
LayneMitch is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 06:01 AM Re: request Object w/ XML File On Desktop
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
No, I'm trying to do it without server side PHP code.
That's not what I was talking about...

If you open your html page from your hard disk, you are using (implicitely) the file:// protocol of the browser.
The browser react differently than when you use the http:// protocol.

What you need to do, is to install a simple web server (just apache, or iis, or the personal web server from windows, whichever. No need for PHP or anything else) and access your html page via http://127.0.0.1/mypage.html
Until so, I remember that you cannot use an ajax request.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-13-2009, 06:45 AM Re: request Object w/ XML File On Desktop
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
This proved to be a bit interesting.

I used

Code:
<?xml version="1.0" encoding="UTF-8"?>
<artists>
  <artist>
    <name>Artist 1</name>
  </artist>
  <artist>
    <name>Artist 2</name>
  </artist>
</artists>
as the XML. After quite a few tutorials and head banging to go along with some for..in's, I was able to get
HTML Code:
<html>
<head>
<script>
var xmlDoc;
var textNode = "textContent";

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

 if (moz_xml) {
   xmlDoc = document.implementation.createDocument("", "", null)
 } else if (ie_xml) {
   textNode = "text";
   xmlDoc = new ActiveXObject("Microsoft.XMLDOM");   
 }
 xmlDoc.async = false;
 xmlDoc.load(file);
}

function beginSearch(the_word, x){
  var xmlArtists = xmlDoc.getElementsByTagName("artists");
  for (var i=0;i<xmlArtists[0].childNodes.length;i++) {
    if (xmlArtists[0].childNodes[i].nodeType != 1) continue;
    var name_field = xmlArtists[0].childNodes[i].getElementsByTagName('name')[0];
    document.getElementById('results').innerHTML += name_field[textNode] + "<br />";
  }
}

importXML("rb_artist2.xml");
</script>
</head>
<body>
<input type="button" onClick="beginSearch('abc',2); return false" value="Click"/>
<div id="results"></div>
</body>
</html>
to work in both the latest versions of FF and IE.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to request Object w/ XML File On Desktop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26752 seconds with 12 queries