HttpServletRequest JAXP DOM:读取 POST 数据
我的 servlet 中有一个 HttpServletRequest 对象 获取发布到它的 XML 文档。我想用 JAXP(不是 JAXB,因为它使用太多磁盘空间 对于我的特定用例)。我需要解析文档 到内存中的 DOM 对象中进行处理。 知道如何从请求对象中解析 POST XML 吗?
谢谢,
约翰·戈奇
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于客户如何发送它。
如果它符合 HTTP
multipart/form-data
标准(就像与 HTML一起使用的那样),则使用 < a href="http://commons.apache.org/fileupload" rel="nofollow noreferrer">Apache Commons FileUpload 或 Servlet 3.0
HttpServletRequest#getParts()
从多部分请求中提取所需的部分。您可以在这里找到一些具体示例:如何将文件上传到服务器使用 JSP/Servlet? 您最终希望得到一个InputStream
。如果它是原始请求正文(即整个请求正文实际上是整个 XML 文件,您经常在滥用 HTTP 协议传输文件的本地低级应用程序中看到这种情况),那么您可以得到它作为一个
InputStream
通过HttpServletRequest#getInputStream()
。无论您使用/选择哪种方式,您都需要确保以某种方式最终得到引用 XML 文件的
InputStream
。这样您就可以按照通常的方式将其提供给 JAXP API,该 API 具有采用InputStream
的方法。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.0HttpServletRequest#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 anInputStream
.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 justHttpServletRequest#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 anInputStream
.