aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet with XML" Watch "Servlet with XML" New topic
Author

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;
try{
writer = new StringWriter();
(new XMLSerializer(writer, new OutputFormat())).serialize(document);
}
catch(Exception e)
{
System.out.println("prasha");
e.printStackTrace();
}
return writer.toString();
}

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

thanks and regards
Prashanth
Ben Souther
Sheriff

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
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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.

Bill
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


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

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
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Take a look at the getInputStream method of ServletRequest:
    http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getInputStream&40;&41;
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Servlet with XML