简体   繁体   中英

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.

What I have: - An XML File with information

What I want: - A simple div, which displays the (with XPath filterd) Content of the XML-File.

I am complete new to PrototypeJS and dont know where to begin, so I appreciate your help.

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
  • fetch the xml file by ajax (easiest way) to have it as an xml document in your javascript
  • parse the xml file with the dedicated API
  • build an HTML representation of the content using text, images, etc. and include it in your 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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