1

I am trying to load an xml file that is on my local system. But I always get Network_err. I do the following.

function LoadXmlDoc(dName)
{
    var xhttp;
    if(window.XMLHttpRequest)
    {
        xhttp = new XMLHttpRequest();
    }
    else
    {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    try
    {       
        xhttp.open("GET", "file.xml", false);
        xhttp.send();
    }
    catch(e)
    {       
        window.alert("Unable to load the requested file.");
        return;
    }
    return xhttp.responseXML;
}

How can I load an xml file that is on my system. all files are in same folder on my pc. Thanks

Sarfraz Ahmed
  • 1,349
  • 6
  • 23
  • 43

3 Answers3

1

Try:

function XMLDoc()
{
if (window.XMLHttpRequest)
  {
    xmlhttp=new XMLHttpRequest();
  }
else
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    };
xmlhttp.open("GET","yourfile",true);
xmlhttp.send();
}

Updated due to simplify

Invoke XMLDoc() and pass your file uri instead of yourfile

Note: Don't forget to run this script on server

nanobash
  • 5,419
  • 7
  • 38
  • 56
  • 1
    here, I make a folder. I put my html, javascript and xml file in that folder. and I run the html. where I make a button on whose event javascript function get called. Thanks – Sarfraz Ahmed Jan 08 '13 at 10:13
  • then how should I give it file name? because if I try to give just "file.xml", it gives the same error. – Sarfraz Ahmed Jan 08 '13 at 10:51
  • @Sarfraz you've to pass instead of `yourfile` file `URL` (where desired file is located) – nanobash Jan 08 '13 at 10:53
  • Thanks @DaHaKa. can you please tell me what will be my file url? if it is in "C:\HTML" folder with name "file.xml". I am new to all this staff. Thanks – Sarfraz Ahmed Jan 08 '13 at 11:19
  • @Sarfraz Is this file in your root directory ? – nanobash Jan 08 '13 at 11:33
  • Yes my all files (.html, .js and .xml) files are in this folder "C:\HTML". I am using windows xp if it matters. Thanks – Sarfraz Ahmed Jan 08 '13 at 11:42
  • @Sarfraz Which server are you using ? – nanobash Jan 08 '13 at 11:44
  • I suppose I am using local server. I just create a html file and a javascript file. put them in a folder. and run the html file in browser. – Sarfraz Ahmed Jan 08 '13 at 11:53
  • @Sarfraz Like I see you haven't have server , in that case you can't accomplish this task. I suggest first understand what server is and how to install. If you are on windows , I suggest install wamp server , here is link => http://www.wampserver.com/en/ – nanobash Jan 08 '13 at 11:57
1

you might need to give proper path of xml file like this

xhttp.open("GET", "file:///C:/file.xml", false);
        xhttp.send();

will do work fo ryou

full code will be like , Read more : Loading XML with Javascript

    var xmlDoc;
    var xmlloaded = false;

    function initLibrary()
    {
        importXML("file:///C:/file.xml");
    }

    function importXML(xmlfile)
    {
        try
        {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", xmlfile, false);
        }
        catch (Exception)
        {
            var ie = (typeof window.ActiveXObject != 'undefined');

            if (ie)
            {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                while(xmlDoc.readyState != 4) {};
                xmlDoc.load(xmlfile);
                readXML();
                xmlloaded = true;
            }
            else
            {
                xmlDoc = document.implementation.createDocument("", "", null);
                xmlDoc.onload = readXML;
                xmlDoc.load(xmlfile);
                xmlloaded = true;
            }
        }

        if (!xmlloaded)
        {
            xmlhttp.setRequestHeader('Content-Type', 'text/xml')
            xmlhttp.send("");
            xmlDoc = xmlhttp.responseXML;
            readXML();
            xmlloaded = true;
        }
    }
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

You can't using XHR due security reasons.

Check this post is very complete answer for you.

Then ckeck the HTML5 API for local files: http://www.html5rocks.com/en/tutorials/file/filesystem/

Community
  • 1
  • 1
Gabriel Gartz
  • 2,840
  • 22
  • 24