How to Download XML File

(Matt) #1

Hi -
I’ve researched this but I’ve not been able to find an answer.

I’m using an API in which one of the calls is to be able to get XML of an embed. I’m able to use the API perfectly fine for most other things.
How might I make this call from a button and have the XML be downloaded as a file?

the call is: getMusicXML()

The embed is located in a window identified as this:

So I basically just have this:

Obviously I need to have some other javascript to get this info in a downloaded XML file.

The particulars about this call from the API are as such:
Description:Returns the score contents in the embed client as a NoteflightXML string.

Usage Restrictions:Unless used by the score’s author, this function requires the score to allow copy and export.

Parameters: None.

Return Value: A NoteflightXML string representation of the score.

The complete API is here

File attached: (69.3 KB)

Any help would be so appreciated.
Thank you!

(Jonathan Deutsch) #2

First, you have you small inconsistency in the above post - you mention both getMusicXML() and getNoteflightXML(); I assume what you really want is getMusicXML(), but the solution is basically the same for both. These are functions that return a promise, so to get at the XML data, you need to structure the code like:

window.scoreView.getMusicXML().done(function(scoreXML) {
    // do something with scoreXML

In your case, it looks like you want the file to be downloaded. So I found this code that takes a string and makes it into a downloadable file (though it looks like this particular solution might not work well with firefox):

       window.scoreView.getMusicXML().done(function(scoreXML) {
       	function download(filename, text) {
		  var element = document.createElement('a');
		  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
		  element.setAttribute('download', filename); = 'none';
		download('score.xml', scoreXML);

I hope that puts you in the right direction!

(Matt) #3

Thank you Jonathan!
Worked perfectly!

I’ve learned so much javascript since using Hype but I just could not figure this one out.

Thank you!