简体   繁体   English

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

[英]Where can I find Java EE 6 XML Libraries

I'm learning Java EE 6 & Jax-RS (haven't started anything about Jax-RS yet) to build an xml api for my work. 我正在学习Java EE 6和Jax-RS(还没有开始关于Jax-RS的任何事情)来为我的工作构建一个xml api。 I have my java controllers setup and I'm looking now to use my models to generate XML output. 我有我的java控制器设置,现在我正在寻找使用我的模型来生成XML输出。 I can't seem to find any XML libraries by searching Google. 我似乎无法通过搜索Google找到任何XML库。 Can someone help point me in the right direction? 有人能指点我正确的方向吗?

If you use JAX-RS, you can use @Produces("application/xml"), then you will have an xml api. 如果您使用JAX-RS,您可以使用@Produces(“application / xml”),然后您将拥有一个xml api。 JAX-RS works by default with JAXB. JAX-RS默认使用JAXB。

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

Java SE already includes XML parsers in three styles: Java SE已经包含三种样式的XML解析器:

  1. SAX – You create one using the SAXParserFactory helper class. SAX - 您使用SAXParserFactory帮助程序类创建一个。 I believe it's the oldest of the APIs and it follows a W3C standard. 我相信它是最古老的API,它遵循W3C标准。 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. 它也很难使用,因为它通过传递一堆回调来调用解析器,你必须自己跟踪文档中的位置,并且StAX可用时几乎没有理由使用它。 It also can't be used to write XML. 它也不能用于编写XML。
  2. DOM – You create a parser using a DocumentBuilderFactory . DOM - 您使用DocumentBuilderFactory创建解析器。 The only one of the APIs to offer random access to the parsed three. 唯一一个提供对已解析的三个随机访问的API。 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. (尽管可以很好地序列化Web服务输出。)Java的实现遵循W3C标准,这使得API有点混乱,因为它避免遵循Java惯用法。 There's two fairly popular alternative DOM-style APIs that are more Java-like, JDOM and dom4j . 有两个相当流行的替代DOM风格的API,更像Java, JDOMdom4j
  3. StAX – the new kid on the block, Java-specific, and my personal favourite. StAX - 块上的新手,特定于Java,以及我个人的最爱。 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. 这使得它比DOM快得多,同时更容易包裹你的头脑。 You start by using XMLInputFactory to read XML and XMLOutputFactory to write it. 首先使用XMLInputFactory读取XML和XMLOutputFactory来编写它。

If you're going to use JAX-RS, you probably don't want to use these "low-level" XML libraries. 如果您打算使用JAX-RS,您可能不希望使用这些“低级”XML库。 JAX-RS should handle parsing requests and formatting responses for you. JAX-RS应该为您处理解析请求和格式化响应。 I believe it uses JAXB for this, also a part of the standard library. 我相信它使用JAXB ,也是标准库的一部分。 JAXB will serialise a Java object automatically based on annotations on the class and its fields / properties that control the serialisation. JAXB将根据类及其控制序列化的字段/属性的注释自动序列化Java对象。

The libraries aren't JavaEE specific. 这些库不是JavaEE特有的。 For generating XML, StAX and JDOM are the most commonly used in my experience. 为了生成XML,StAX和JDOM是我的经验中最常用的。 StAX is part of the JDK. StAX是JDK的一部分。 They also do XML parsing and validation. 他们还进行XML解析和验证。

A couple of tutorials: 1 2 几个教程: 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 XY). StAX使用基于事件的模型(读取文档,当您看到元素X执行某些操作时),并且毫无疑问,JDOM使用类似于DOM的语法(查找元素XY)。 JDOM loads the entire document tree into memory, which eats up memory but performs faster. JDOM将整个文档树加载到内存中,这会占用内存但执行速度更快。

Here's an overview comparing the two tools and JAXB. 这是一个比较两个工具和JAXB的概述。

Personally I like the simplicity of Simple XML http://simple.sourceforge.net/ 就个人而言,我喜欢Simple XML的简单性http://simple.sourceforge.net/

Also you could consider using JAXB that comes with Java EE6. 您还可以考虑使用Java EE6附带的JAXB。 http://www.oracle.com/technetwork/articles/javase/index-140168.html http://www.oracle.com/technetwork/articles/javase/index-140168.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM