最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Can not load xml file in javascript - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Jan 8, 2013 at 7:54 Sarfraz AhmedSarfraz Ahmed 1,3996 gold badges24 silver badges45 bronze badges 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? – kannanrbk Commented Jan 8, 2013 at 7:56
  • open fire bug and see that you are getting to the correct location. – AMember Commented Jan 8, 2013 at 7:58
  • is that work for you ??? – Pranay Rana 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 – Sarfraz Ahmed Commented Jan 8, 2013 at 10:06
Add a ment  | 

3 Answers 3

Reset to default 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

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;
        }
    }

You can't using XHR due security reasons.

Check this post is very plete answer for you.

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

发布评论

评论列表(0)

  1. 暂无评论