File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet returning an XML

 
Jahnavi Kondapaneni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jahnavi Kondapaneni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please show me a sample example?
Or any links which hava a sample code.
 
Jahnavi Kondapaneni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jahnavi Kondapaneni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right its just sending back the xml data.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably this might be a better approach

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1601
5
Android Java Java ME
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just look at the second code on this page

http://www.javazoom.net/services/newsletter/xmlgeneration.html
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: the number one rule at JavaRanch is "Be nice". So be nice.]
 
Jahnavi Kondapaneni
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic