File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet returning an XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet returning an XML" Watch "Servlet returning an XML" New topic
Author

Servlet returning an XML

Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
Hi all,

I want a servlet to load an XML and the response from the servlet shud be an XML.
How can I do this?
Looking forward for your response.
Thanks in advance.


Regards,
Janu
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well if you already have the XML document, then you can just read it using java.io classes and then send it as a response line by line and set the content type to text/xml. If you want to build the XML document, then you can build it just like HTML document (but then you'll have to take care of creating a well formed document) or use the W3C API to build a well formed XML document...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
Can you please show me a sample example?
Or any links which hava a sample code.
Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
In case I already have a sample xml and load an XML using servlet and send the response in form of XML for this can you show me a sample code for reference?
Or any links which hava a sample code.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6



Swastik
Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
But this code is just printing the XML right?
Nowhere do I see that setContent("text/xml") because we are trying to load an XML and receive the response in form of an XML.


Swastik Dey wrote:
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Right its just sending back the xml data.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Swastik gave a perfect example. Just add setContent("text/xml") to it. Just to add, if you have problems accessing the xml file, then you'll need to give the path to the xml file relative to the root of the web app and then use getServletContext().getRealPath("xmlFileRelativePath/xmlfileName.xml");

Eg if the xml file is in folder /myWebApp/xmls/myXml.xml and myWebApp is the root of the application, then you'll use this statement

FileInputStream fis=new FileInputStream(getServletContext().getRealPath("/xmls/myXml.xml"));

And if you want to build a XML document, then the best way would be to use the W3C DOM API. It works just javascript DOM so it won't be a problem to use it. If you still have any problems, then try searching on the internet or just create a sample App and then we'll help you
Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
Hi please look through the following code and tell me if its the right way to create an XML in a servlet.
I placed this code within the doGet method
response.setContentType("text/xml");
PrintWriter out = response.getWriter();
out.println("<?xml version='1.0' encoding='ISO-8859-1'?>");
out.println("<company>");
out.println("<compname>" +companyname+ "</compname>");
out.println("<contname>" +contactname+ "</contname>");
out.println("<address>" +address+ "</address>");
out.println("<city>" +city+"</city>");
out.println("<country>" +country+ "</country>");
out.println("</company>");
When I do this and run my application I am getting a blank page.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

probably this might be a better approach

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Jahnavi try this

out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");

instead of this

out.println("<?xml version='1.0' encoding='ISO-8859-1'?>");

rest looks fine. Also flush the response as Swastik said but the method he told and you are using won't make much difference to your problem...
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

Thats true Ankit, but I have a feeling that instead of writing to response repeatedly, writing the entire content to the response at time might be little better, correct me if I am getting wrong.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well swastik I don't think that makes too much difference. If you write a JSP page, it also get's translated into a Servlet which contains repeated out.write() method calls. Also the point of concern right now is that Jahnavi is not able to see anything in the response. And whichever way she (I think Jahnavi is a female name) uses, this is not the proper way to do this. W3C DOM API must be used for this purpose. I will make a small program to show how to do when I get time...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Just look at the second code on this page

http://www.javazoom.net/services/newsletter/xmlgeneration.html
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
[Edit: the number one rule at JavaRanch is "Be nice". So be nice.]


If you want something you never had do something which you had never done
Jahnavi Kondapaneni
Ranch Hand

Joined: Jan 25, 2008
Posts: 56
Hey guys thank you all so much for your help.
I am still working on it.
I am referring to the link given by Ankit will update you all once I am done with this.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hey, partner!

You have to somehow read the content of this XML file and flush it to the HttpServeltResponse.getOutputStream() in your doGet()/doPost() method. I have already done this once using JAXB, so this is one of the possible ways. The other way is to do what Swastik suggested.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41129
    
  45
Swastik Dey wrote:

Instead of using a Reader for the file and a Writer for sending the output, use the FileInputStream directly and copy the bytes to the OutputStream you get from res.getOutputStream(). That way you don't have to worry about the encoding of the XML file (which, invariably, at some point, will NOT be the platform default encoding, at which point the above code will get you in trouble).


Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Servlet returning an XML
 
Similar Threads
How to get SOAP response as XML in Client Struts or Servlet. ?
Export to xls file - Download file using AJAX
Transforming Servlet Response
Servlet Response for AJAX
Ajax in Websphere