HttpServletRequest JAXP DOM:读取 POST 数据

发布于 2024-12-28 06:43:10 字数 176 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2025-01-04 06:43:10

这取决于客户如何发送它。

如果它符合 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.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文