0

I download a XML file from the server using a XML Object. My task is to display that object inside a downloaded file. This is my achievement until now:

My code runs inside the read operation success callback. This is working:

var string = "abs"; 
var fileName = "docxml";

var uri = 'data:text/xml;charset=utf-8,'+ encodeURI(string);

var link = document.createElement("a");    
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".xml";

document.body.appendChild(link);
link.click();
document.body.removeChild(link);

If I build the URI this way it is not working.

var uri = 'data:text/xml;charset=utf-8,'+ encodeURI(oData.results[0].xmlmessage); 

How can I access the XML content of an object?

matbtt
  • 4,230
  • 19
  • 26
CIC92
  • 15
  • 9

1 Answers1

0

You odatamodel will probably be in JSON. To get JOSN output, you can do the following: Provided, the rest of your code is correct, try this.

var uri = 'data:text/xml;charset=utf-8,'+ JSON.stringify(oData);

In case you want XML, you should either set your model to XML or parse the oData to XML. A search on stackoverflow will give you some good results.

Community
  • 1
  • 1
Matti.b
  • 360
  • 3
  • 12
  • with your answer I get all the service with all it's oData parameters but I only need to display oData.results[0].xmlmessage. I tried to put var uri = 'data:text/xml;charset=utf-8,'+ JSON.stringify(oData.results[0].xmlmessage); but it only displays inside the file '' two quotation marks. – CIC92 Oct 06 '16 at 13:25
  • That is because your respone probably has no property xmlmessage. You should evaluate what your response gives back and then only pass what you need. There is no representation of JSON data in xml format with using ".xmlmessage". – Matti.b Oct 06 '16 at 13:30
  • I checked my service, I have it in the service I put it on a JSON Viewer and I have content inside... – CIC92 Oct 06 '16 at 15:07
  • OK I found the problem, you were right I am able now to put the string in the file but because is an xml object I only display it when I open it with notepad or something if I open the file with a browser (becouse xml files request a browser for default) the file is empty, only displays a white background. – CIC92 Oct 06 '16 at 16:39
  • I have to display the xml content as xml content in a brower, I can't use JSON.stringify becouse I can view the content only with notepad, the browser doesn't know how to read the string – CIC92 Oct 06 '16 at 16:41