This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet with XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet with XML" Watch "Servlet with XML" New topic

Servlet with XML

Prashanth Bhanu
Ranch Hand

Joined: Sep 25, 2003
Posts: 110
hi friends,

I've a client(Swing) formed an XML(using DOM) and sent it to servlet .How do i extract my DOM object from the HttpRequest Object ???

below is the piece of code which converts document object to string before sending to servlet

public static String convertDOM2String(Document document){
StringWriter writer=null;
writer = new StringWriter();
(new XMLSerializer(writer, new OutputFormat())).serialize(document);
catch(Exception e)
return writer.toString();

Can some one help me in this regard.Thanks in advance

thanks and regards
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

How did you send it, as an HTTP form parameter or it the post, itself, an xml document?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12760
The most basic idea is to use the request getInputStream method to get a stream readable by a parser. However, as Ben suggested, there are complications depending on how you sent it which may prevent this.

For example, note that if you use any request getParameter method, the servlet engine will have already read the stream and you wont be able to get it.

Prashanth Bhanu
Ranch Hand

Joined: Sep 25, 2003
Posts: 110
hi friends,

Here is a piece of code which i used to build XML and sent to a servlet after serializing it...
thanks in advance

[ October 23, 2006: Message edited by: Ben Souther ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.
Prashanth Bhanu
Ranch Hand

Joined: Sep 25, 2003
Posts: 110
hi ,

Thanks,below code generates the DOM object,Serializes it and sends the serialized DOM object to TestServlet.I want to know how do i recover my DOM object from Request object at the server side i mean in servlet.
I tried using getInputstream() but it dint work ...please advice..thanks in advance

[list]//below 7 lines about opening a connection and building the DOM object
  • //below lines of codes creating root element,nodes and values

  • //below code creating 1st node and value

  • //below code creating 2nd node and value

  • //below code creating 3rd node and value

  • //Dom2string method is used to serialize and convert the DOM object to a string

  • thanks,
    Ben Souther

    Joined: Dec 11, 2004
    Posts: 13410

    Take a look at the getInputStream method of ServletRequest:;&41;
    I agree. Here's the link:
    subject: Servlet with XML
    Similar Threads
    Making JAXP spit out XML
    about document2string
    Document object into Database
    Converting an XML Node to XML String/Text
    DOM and JAVA