【问题标题】:Accessing xml file in local system [duplicate]访问本地系统中的 xml 文件 [重复]
【发布时间】:2014-10-06 17:34:43
【问题描述】:

我的本​​地系统中有一个 xml 文件(不在服务器上)。我想用javascript访问它来解析它,我该怎么做?我尝试提供本地文件位置。

if (window.XMLHttpRequest)
  {
      xmlhttp=new XMLHttpRequest();
  }

xmlhttp.open("GET","C:\Users\amit.m2\Desktop\metrics.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

【问题讨论】:

  • @quentin 我知道有人问过同样的问题,但是答案很抽象,并没有帮助我解决我的问题。由于我是 javascript xml 和 ajax 的新手,因此期待一个详细的程序来解决这个问题。
  • 重复问题的答案一点也不抽象。
  • @quentin 如果你觉得有详细的答案,请与我分享。

标签: javascript xml xml-parsing


【解决方案1】:
  $.ajax({
          url: 'input.xml', // Name of the file in the local
          dataType: "xml", // input file type
          success: parse, // Call the parse function on success`enter code here` response
     error: 
        function(){
            alert("Unable to load or something is Wrong !!!");
         }
        });

      function parse(document){
      $(document).find("Inputdata").each(function(){
        // Business logic goes here
      });
     }

【讨论】:

  • 这是什么?看起来你已经抓住了一堆对你有用的代码,而没有试图理解为什么 OPs 代码对它们不起作用(因此它可能通过消除错误来解决问题,或者它可能不是因为问题可能是与代码无关(例如同源策略)(在这种情况下,它不会))。无论哪种方式,它都完全没有任何解释,读起来就像一个找不同的游戏,而不是一个答案。
猜你喜欢
  • 2016-11-22
  • 2016-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多