通过 HTTP 输出 Java DOM

发布于 2024-12-27 11:05:33 字数 149 浏览 2 评论 0原文

不久前,我发现了一种通过 HTTP 连接从 Servlet 发送 XML 文档的 DOM 表示形式的方法。 AFAIK 为此需要 DOM 3 LS (LoadStore),但 StackOverflow 上显示如何执行此操作的线程似乎已经消失。

谁能告诉我该怎么做吗?

Not so long ago I found a way of sending a DOM representation of an XML document out over an HTTP connection from a Servlet. AFAIK the DOM 3 LS (LoadStore) is needed for this, but the thread on StackOverflow showing how to do it seems to have disappeared.

Can anyone please show me how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

百变从容 2025-01-03 11:05:33

使用 JAXP:

response.setHeader("Content-Type", "application/xml");
OutputStream out = response.getOutputStream();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(out));

您可能想使用 JAX RS(从未使用过):

@Path("/foo.xml")
public class MyResource {
  @GET @Produces(MediaType.APPLICATION_XML)
  public Source asXml() {
     // TODO, get your DOM somehow
     return new DOMSource(dom);
  }
}

Use JAXP:

response.setHeader("Content-Type", "application/xml");
OutputStream out = response.getOutputStream();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), new StreamResult(out));

You might want to use JAX RS instead (never used it):

@Path("/foo.xml")
public class MyResource {
  @GET @Produces(MediaType.APPLICATION_XML)
  public Source asXml() {
     // TODO, get your DOM somehow
     return new DOMSource(dom);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文