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

4
  • 1) What is the server url? 2) How you tried? 3) If you are tried using local file system ? . What is the system directory path ? . How you tried in browser? Commented Jan 8, 2013 at 7:56
  • open fire bug and see that you are getting to the correct location. Commented Jan 8, 2013 at 7:58
  • is that work for you ??? Commented Jan 8, 2013 at 8:12
  • 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 Commented Jan 8, 2013 at 10:06

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

9 Comments

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
then how should I give it file name? because if I try to give just "file.xml", it gives the same error.
@Sarfraz you've to pass instead of yourfile file URL (where desired file is located)
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 Is this file in your root directory ?
|
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;
        }
    }

1 Comment

I put my file in C directory. Run your given code but it still gives the same error. NETWORK_ERR
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.