在哪里可以找到 Java EE 6 XML 库

发布于 2024-12-20 17:29:53 字数 154 浏览 2 评论 0原文

我正在学习 Java EE 6 & Jax-RS(尚未开始任何有关 Jax-RS 的工作)为我的工作构建 xml api。我已经设置了 java 控制器,现在正在寻找使用我的模型来生成 XML 输出。我似乎无法通过搜索 Google 找到任何 XML 库。有人可以帮我指出正确的方向吗?

I'm learning Java EE 6 & Jax-RS (haven't started anything about Jax-RS yet) to build an xml api for my work. I have my java controllers setup and I'm looking now to use my models to generate XML output. I can't seem to find any XML libraries by searching Google. Can someone help point me in the right direction?

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

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

发布评论

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

评论(4

独木成林 2024-12-27 17:29:53

如果你使用JAX-RS,你可以使用@Produces("application/xml"),那么你将拥有一个xml api。
JAX-RS 默认与 JAXB 配合使用。

请参阅http://jersey.java.net/nonav/documentation/最新/jax-rs.html#d4e318

If you use JAX-RS, you can use @Produces("application/xml"), then you will have an xml api.
JAX-RS works by default with JAXB.

See http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e318

潜移默化 2024-12-27 17:29:53

Java SE 已经包含三种样式的 XML 解析器:

  1. SAX – 您可以使用 SAXParserFactory 帮助器类。我相信它是最古老的 API,并且遵循 W3C 标准。它也相当难以使​​用,因为它是通过传递一堆回调供解析器调用来使用的,您必须自己跟踪自己在文档中的位置,并且随着 StAX 的可用,几乎没有理由使用它。它也不能用于编写 XML。
  2. DOM – 您创建一个使用 DocumentBuilderFactory。唯一一个提供对已解析的三个 API 的随机访问的 API。它也是最慢且占用内存最多的,因此如果您需要处理大型文档,则不建议使用它。 (不过,序列化 Web 服务输出应该没问题。)Java 的实现遵循 W3C 标准,这使得 API 有点混乱,因为它避免遵循 Java 习惯用法。有两个相当流行的替代 DOM 风格的 API,它们更像 Java:JDOMdom4j
  3. StAX – 新来的on the block,Java 特定的,也是我个人最喜欢的。它的工作原理是让你在一系列事件中前进。这使得它比 DOM 更快,同时更容易理解。首先使用 XMLInputFactory 来读取XML 和 XMLOutputFactory 到写下来。

如果您打算使用 JAX-RS,您可能不想使用这些“低级”XML 库。 JAX-RS 应该为您处理解析请求和格式化响应。我相信它使用 JAXB为此,也是标准库的一部分。 JAXB 将根据类及其控制序列化的字段/属性的注释自动序列化 Java 对象。

Java SE already includes XML parsers in three styles:

  1. SAX – You create one using the SAXParserFactory helper class. I believe it's the oldest of the APIs and it follows a W3C standard. It's also fairly difficult to use because it's used by passing a bunch of callback for the parser to invoke you have to keep track of where you are in the document yourself, and with StAX being available there's pretty much no reason to use it. It also can't be used to write XML.
  2. DOM – You create a parser using a DocumentBuilderFactory. The only one of the APIs to offer random access to the parsed three. It's also the slowest and uses the most memory, so it's not recommended if you need to handle huge documents. (Should be fine to serialise web service output though.) Java's implementation follows the W3C standard which makes the API a little confusing since it refrains from following Java idioms. There's two fairly popular alternative DOM-style APIs that are more Java-like, JDOM and dom4j.
  3. StAX – the new kid on the block, Java-specific, and my personal favourite. It works by letting you advance through a stream of events. This makes it much faster than DOM while much easier to wrap your head around. You start by using XMLInputFactory to read XML and XMLOutputFactory to write it.

If you're going to use JAX-RS, you probably don't want to use these "low-level" XML libraries. JAX-RS should handle parsing requests and formatting responses for you. I believe it uses JAXB for this, also a part of the standard library. JAXB will serialise a Java object automatically based on annotations on the class and its fields / properties that control the serialisation.

握住我的手 2024-12-27 17:29:53

就我个人而言,我喜欢 Simple XML 的简单性 http://simple.sourceforge.net/

您也可以考虑使用 JAXB Java EE6 附带的。 http://www.oracle.com/technetwork/articles/javase/index -140168.html

Personally I like the simplicity of Simple XML http://simple.sourceforge.net/

Also you could consider using JAXB that comes with Java EE6. http://www.oracle.com/technetwork/articles/javase/index-140168.html

指尖凝香 2024-12-27 17:29:53

这些库不是特定于 JavaEE 的。根据我的经验,对于生成 XML,StAX 和 JDOM 是最常用的。 StAX 是 JDK 的一部分。他们还进行 XML 解析和验证。

一些教程: 1 2

StAX 使用基于事件的模型(阅读文档,当您看到元素 X做某事),毫不奇怪,JDOM 使用与 DOM 类似的语法(查找元素 XY)。 JDOM 将整个文档树加载到内存中,这会占用内存但执行速度更快。

这里概述了这两个工具和 JAXB 的比较。

The libraries aren't JavaEE specific. For generating XML, StAX and JDOM are the most commonly used in my experience. StAX is part of the JDK. They also do XML parsing and validation.

A couple of tutorials: 1 2

StAX uses an event based model (read the document and when you see element X do something) and unsuprisingly JDOM uses syntax similar to DOM (find element X.Y). JDOM loads the entire document tree into memory, which eats up memory but performs faster.

Here's an overview comparing the two tools and JAXB.

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