用于生成/使用 REST 服务的最佳 Java XML 解析器

发布于 2024-09-27 14:19:58 字数 268 浏览 3 评论 0原文

当前使用 Java 内置的 XML DOM 库进行创建/解析。丑陋的是,即使有很多辅助类和方法,性能和内存使用率也很糟糕。

在生成和使用 REST 服务方面,处理 XML 的最佳 Java 工具或框架是什么?

我使用的服务使用 JAXB。我能够使用他们的课程并如此方便、轻松地使用他们的服务。它很漂亮,但 JAXB 在大多数情况下仍然很痛苦。所以我研究了 StAX 和 VTD-XML。 StAX 已经大约 4 年没有更新了。 VTD-XML 是 Java 中 XML 处理的最先进技术吗?

Currently using Java's built-in XML DOM libraries for creation/parsing. Ugly, even with lots of helper classes and methods, and performance and memory usage sucks.

What's the best Java tool or framework for dealing with XML in regards to producing and consuming REST services?

A service I use uses JAXB. I was able to use their classes and so conveniently and easily consume their services. It was beautiful, but JAXB is still quite a pain in most circumstances. So I looked at StAX and VTD-XML. StAX hasn't been updated in about 4 years. Is VTD-XML the state of the art in XML processing in Java?

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

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

发布评论

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

评论(3

甜警司 2024-10-04 14:19:58

您应该能够编写 JAXB 带注释的类,Jersey 支持这些开箱即用的类并自动为您解析它们。它可能不是性能最好的解决方案,但它是一个非常好的干净的应用程序。

除非你能证明你有性能问题(并且可以追踪到 JAXB),否则我不会担心编组/解组开销。

编辑:
JAXB 带注释的类可以非常简单。 XML 建模;

<customer>
  <name>Fred</name>
  <email>[email protected]</email>
  <id>12345</id>
</customer>

就是这么简单;

@XmlRootElement
public class Customer
{
  private String name;
  private String email;
  private long id;

  public Customer()
  {
  }

  //getters and setters
}

然后你可以像这样编写服务;

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/getCustomer")
public Customer getCustomer()
{
  Customer c = new Customer();
  c.setName("Fred");
  c.setEmail("[email protected]");
  c.setId(12345);
  return c;
}

..和这样的客户;

Client client = Client.create();
WebResource resource = client.resource("myHost/getCustomer");
Customer fred = resource.get(Customer.class):

非常简单。

You should be able to write JAXB annotated classes, Jersey supports these out of the box and automatically parses them for you. It might not be the most performant solution but is makes for a really nice clean app.

Unless you can prove you have a performance problem (and can trace it to JAXB) then I would't worry about the marshalling/unmarshalling overhead.

Edit:
The JAXB annotated classes can be really simple. Modelling XML like;

<customer>
  <name>Fred</name>
  <email>[email protected]</email>
  <id>12345</id>
</customer>

is as easy as this;

@XmlRootElement
public class Customer
{
  private String name;
  private String email;
  private long id;

  public Customer()
  {
  }

  //getters and setters
}

You can then write services like this;

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/getCustomer")
public Customer getCustomer()
{
  Customer c = new Customer();
  c.setName("Fred");
  c.setEmail("[email protected]");
  c.setId(12345);
  return c;
}

..and clients like this;

Client client = Client.create();
WebResource resource = client.resource("myHost/getCustomer");
Customer fred = resource.get(Customer.class):

Its beautifully simple.

书信已泛黄 2024-10-04 14:19:58

StAX 已经很长时间没有更新了,因为它不需要更新。 XML 格式没有改变,StAX 是该格式到 API 的相当简单的映射。作为低级 XML API,它非常容易使用,而且我相信非常高效;然而,它在很大程度上是一个低级 API。它(非常粗略地)相当于用于写入二进制数据的 DataInputStream 和 DataOutputSteam 的 XML;使用它编写的代码必须在元素、属性等概念级别上编写。我没有使用过 VTD-XML,但看起来差不多。

如果您想使用更高级别的工具,请查看一些 XML 序列化工具,例如 XStream - 您能否编写一些类,通过适当的自定义转换,可以从传入的数据中读取这些类?那么,您是否可以对略有不同的输入使用相同的类,而忽略缺失的字段?事实上,您可以在 JAXB 中使用“忽略缺失的 XML 元素”方法吗?我不知道它对模式有多严格,也不知道您的输入是否可以以这种方式处理。我经常在黑暗中拍摄。

StAX hasn't been updated in a long time because it hasn't needed to be. The XML format hasn't changed, and StAX is a fairly simple mapping of that format into an API. As low-level XML APIs go, it's pretty easy to use, and i believe quite efficient; however, it is very much a low-level API. It's (very roughly) the XML equivalent of DataInputStream and DataOutputSteam for writing binary data; code written using it will have to be written at the conceptual level of elements, attributes, and so on. I haven't used VTD-XML, but it looks about the same.

If you want to use something higher-level, then have a look at some of the XML serialisation tools, like XStream - could you write some classes which, with the appropriate customisation of the conversion - could be read from your incoming data? Could you then use the same classes for subtly different inputs, with missing fields simply being ignored? In fact, could you use the 'missing XML elements are ignored' approach with JAXB? I don't know how strict it is about schemas, or whether your inputs can be approached in that way. I'm very much shooting in the dark here.

回眸一遍 2024-10-04 14:19:58

我可以推荐 Spring MVC 3+。对 REST 服务有开箱即用的支持。创建 REST 服务需要几分钟如果您了解 Spring...

http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

I can recommend Spring MVC 3+. There is out-of-box support for REST services. It takes couple of minutes to create REST service If you know Spring...

http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/

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