简体   繁体   English

使用JavaScript提取XML数据

[英]Pulling XML Data using JavaScript

I'm trying to dispay the one node eg "title" on a internal html page but I need it to pull the data from an XML document that is saved on my Desktop but it doesn't seem to be finding the XML document. 我正在尝试在内部html页面上放弃一个节点,例如“ title”,但我需要它从保存在我的桌面上的XML文档中提取数据,但似乎找不到XML文档。 I'm not to sure what to add or how to combat this problem? 我不确定要添加什么或如何解决此问题? Any help would be much appreciated! 任何帮助将非常感激! Many thanks 非常感谢

 var parser, xmlDoc; var text = loadXMLDoc("/Desktop/test.xml"); parser = new DOMParser(); xmlDoc = parser.parseFromString(text,"text/xml"); document.getElementById("demo").innerHTML = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue; 

First of all you can't access any files from the absolute path of your system due to security reasons. 首先,由于安全原因,您无法从系统的绝对路径访问任何文件。 you must need to upload file at first place, there after you can process the XML content. 您必须首先上传文件,然后才能处理XML内容。

follow link below tried to create small snippet for you. 按照下面的链接尝试为您创建小片段。

HTML: HTML:

<div>
  <label id="txtFileInfo"></label>
  <input id="xmlFile" type="file">
</div>

JS: JS:

(function($){

  $('#xmlFile').unbind('change')
    .bind('change', function(e){
    if(this.files.length > 0){
      var file = this.files[0];
      if(file.type == 'text/xml'){
          setTimeout(function(){
              console.log('Reader');
             var reader = new FileReader();
            reader.addEventListener('load', function(){
              console.log(reader.result);
              parser = new DOMParser();
              xmlDoc = parser.parseFromString(reader.result, file.type);
              console.log(xmlDoc);
            });
            reader.readAsDataURL(file);
          }, 100);
         }
       // $('#txtFileInfo').text('Type: '+ file.type);
     }
  });


 })($);

https://codepen.io/smtgohil/pen/gGWoRe https://codepen.io/smtgohil/pen/gGWoRe

All the best. 祝一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM