Search code examples
javascriptxmljsonprototypejs

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


Solution

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