1

I have a method in my Controller which looks like this:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody String getNameAsXML(HttpServletRequest httpRequest, @RequestParam("name") String name)
{
    ... some logic goes here to get the data from the db 
     by name and convert it to valid xml string
    ...
        return xmlString;
}

xmlString is a String representation of the xml I want to return.

When I run this method I can see the xml in the browser however I would like instead to give the user a download popup to allow him to download this as an xml file instead of showing it inside the browser.

I thought about returning MultipartFile instead of String but not quite sure how to do it.

Joly
  • 3,218
  • 14
  • 44
  • 70

3 Answers3

3

This can be achieved by setting the "Content-Disposition" in the response header to "attachment=". It's good practice to set the response type to the correct MIME type ("text/xml"). This might already be done automatically, however.

Seidr
  • 4,946
  • 3
  • 27
  • 39
  • I am also facing the same scenario, I mean, I can able to see the xml in the browser but only values(without corresponding xml tags). I can able to see entire xml in page source of browser. For using response.setHeader("Content-Disposition", "attachment="); do I need to have any file in my classpath or do I have to write the content of xml string into that file??? – Sudip7 Nov 19 '14 at 12:21
  • The value of attachment simply tells the browser what to initially save the file as. It sounds like you need to specify the correct MIME type, so that the browser will render the XML document, instead of parsing it as HTML. – Seidr Nov 19 '14 at 13:39
  • Please correct me in m wrong in any way. m posting a snapshot of my code. `public @ResponseBody String getVMdata(...) { ... .. HttpEntity resEnity = response.getEntity(); content = EntityUtils.toString(resEnity); response.setHeader("Content-Type", "application/xml"); response.setHeader("Content-Disposition", "attachment="); return content; }`. The value of content is in the format of `...`. – Sudip7 Nov 20 '14 at 04:17
  • I created a dom tree from xml string. I stored that to local folder, now m trying to add that file as attachment to response, but not able to find any link in my browser. Please have a look to comment section of @Blaze-Core for more clarity. – Sudip7 Nov 20 '14 at 07:46
0

you can create xml file from string in java as follow:

 public static void main(String[] args) {  
        String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></soap:Envelope>";  

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  

        DocumentBuilder builder;  
        try  
        {  
            builder = factory.newDocumentBuilder();  

            // Use String reader  
            Document document = builder.parse( new InputSource(  
                    new StringReader( xmlString ) ) );  

            TransformerFactory tranFactory = TransformerFactory.newInstance();  
            Transformer aTransformer = tranFactory.newTransformer();  
            Source src = new DOMSource( document );  
            Result dest = new StreamResult( new File( "xmlFileName.xml" ) );  
            aTransformer.transform( src, dest );  
        } catch (Exception e)  
        {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  

then provide link to that file so that user can download it.

refer http://www.coderanch.com/t/512978/java/java/convert-string-xml-file-java

also refer http://www.java.happycodings.com/XML/code17.html

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • I followed your instruction in creating a dom from xml string, here is a snapshot. `public @ResponseBody String getVMdata(...) { ... .. HttpEntity resEnity = response.getEntity(); content = EntityUtils.toString(resEnity . . . Result dest = new StreamResult(new File("C:\\sample1.xml")); aTransformer.transform(src, dest); response.setHeader("Content-Type", "application/xml"); response.setHeader("Content-Disposition", "attachment;filename=C:\\sample1.xml); return content }`. But m not able to see any link in the browser. what is wrong in my coding??? – Sudip7 Nov 20 '14 at 07:32
0

I ended up adding response.setHeader("Content-Disposition", "attachment; filename=test.xml");to the Controller method above and it is working fine.

I'm not 100% pleased with this solution as I was looking for something more Spring configurable rather than hack into the response object so if anyone has a better idea then please share.

Thank you for the responses above but I think they're just as hack as mine :)

Joly
  • 3,218
  • 14
  • 44
  • 70