HttpServletRequest JAXP DOM: reading POST data

631 Views Asked by At

I have an HttpServletRequest object in my servlet which obtains an XML document posted to it. I would like to use JAXP (not JAXB becuase for one it uses too much disk space for my particular use case). I need to parse the document into a DOM object in memory where it will be processed. Any idea of how to parse the POST XML from the request object?

Thanks,

John Goche

1

There are 1 best solutions below

0
On BEST ANSWER

That depends on how the client has sent it.

If it's conform the HTTP multipart/form-data standard (like as is been used together with the HTML <input type="file">), then use Apache Commons FileUpload or Servlet 3.0 HttpServletRequest#getParts() to extract the desired part from the multipart request. You can find some concrete examples here: How to upload files to server using JSP/Servlet? You'd ultimately like to end up with an InputStream.

If it's the raw request body (i.e. the entire request body is actually the whole XML file, you see this often in homegrown low level applications which are abusing the HTTP protocol for transferring files), then you can get it as an InputStream by just HttpServletRequest#getInputStream().

Whatever way you use/choose, you'd need to ensure that you somehow end up with an InputStream referring the XML file. This way you can feed it to the JAXP API the usual way, which has methods taking an InputStream.