简体   繁体   English

如何在Prototype JS中读取和解析(本地)XML文件?

[英]How to read and parse (local) XML-file in Prototype JS?

I have to create a module in a specific project, which already uses PrototypeJS. 我必须在已经使用PrototypeJS的特定项目中创建一个模块。

What I have: - An XML File with information 我所拥有的:-具有信息的XML文件

What I want: - A simple div, which displays the (with XPath filterd) Content of the XML-File. 我想要的是:-一个简单的div,它显示XML文件的内容(使用XPath过滤)。

I am complete new to PrototypeJS and dont know where to begin, so I appreciate your help. 我是PrototypeJS的新手,也不知道从哪里开始,所以感谢您的帮助。

Blessing chris 克里斯祝福

If by "local" you mean "client-side", you will have to : 如果“本地”是指“客户端”,则必须:

  • include a file input for the user to upload the xml file to your server 包括供用户输入的文件,以将xml文件上传到您的服务器
  • fetch the xml file by ajax (easiest way) to have it as an xml document in your javascript 通过ajax提取xml文件 (最简单的方法)以将其作为javascript中的xml文档
  • parse the xml file with the dedicated API 使用专用API解析xml文件
  • build an HTML representation of the content using text, images, etc. and include it in your div. 使用文本,图像等构建内容的HTML表示,并将其包含在div中。

edit: to clarify the fetch part, here is how you can do it using Prototype: 编辑:澄清获取部分,这是您可以使用原型进行的操作:

new Ajax.Request('myfile.xml', {
  onSuccess: function(transport) {
    myParseXml(transport.responseXML);
  },
  onFailure: function(transport) {
    alert('Failure! Status code '+transport.status+' ('+transport.statusText+')');
  }
);

function myParseXml(xmlDoc) {
  var root = xmlDoc.documentElement;
  ...
}

Try: 尝试:

<xml src="MyData.xml" id="mydata" >
var mydata = document.getElementById('mydata');

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

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