是否有用于 XML 绑定的 JavaScript API - 类似于 Java 的 JAXB? [英] Is there a JavaScript API for XML binding - analog to JAXB for Java?

查看:33
本文介绍了是否有用于 XML 绑定的 JavaScript API - 类似于 Java 的 JAXB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,我们经常使用 JAXB2.Object<->XML 映射在 Java 类中被定义为注解:

In Java, we work a lot with JAXB2. Object<->XML mappings are defined as annotations in Java classes:

@XmlRootElement(name="usertask", namespace="urn:test")
public class UserTask
{
    @XmlElement(namespace="urn:test")
    public String getAssignee() { ... }

    public void setAssignee(String assignee) { ... }
}

JAXB 运行时可以读取这些注释并创建解组器以将 XML 解析为对象实例或将对象编组为 XML.

JAXB runtime can read these annotations and create unmarshaller to parse XML into an object instance or marshall an object into XML.

JAXB 提供了一个模式编译器 (XJC),它可以从 XML 模式中生成带注释的类,这是另一个很棒的特性.

JAXB ships a schema compiler (XJC) which can generate annotated classes out of XML Schemas, which is another great feature.

最近,我们一直在大量使用客户端 JavaScript.我们还需要在那里进行 XML 处理.例如,我们需要解析像this one这样的WPS文档.这些文档还符合不同的 XML 模式(这里是 示例 XML 的 WPS 1.0.0 架构).使用 JavaScript 对象而不是 XML 会很棒,这会节省大量的精力.在某些情况下,我们可以使用基于 JSON 的解决方案,例如 DWR,但在许多情况下,我们必须在客户端处理 XML——一边.

Lately we've been working a lot with client-side JavaScript. An we also need XML processing there. For example, we need to parse WPS documents like this one. These documents also comply to different XML schemas (here's the WPS 1.0.0 schema for the sample XML). It would be great to work with JavaScript objects instead of XML, this saves really huge amount of effort. In some cases we can use JSON-based solutions like DWR, but in many cases we do have to process XML on the client-side.

我的问题是:

是否有一些类似于 JAXB 的 JavaScript?

某种工具可以将 XML 模式编译成某种 XML<->对象映射并提供运行时在 XML 和 JavaScript 对象之间进行转换?

Some tool which would compile an XML Schema into some XML<->object mapping and provide a runtime to convert between XML and JavaScript objects?

我可以很容易地想象以如下形式生成的映射:

I could easily imagine mappings generated in a form like:

UserTask = new JSXML.XmlRootElement({
  name: "usertask",
  namespace: "urn:test",
  properties: [
    {
      assignee: new JSXML.XmlElement({
        name: "assignee",
        namespace: "urn:test",
        type: new JSXML.XSD.String()
      })
    }
  ]
});

这应该足以构建解组器或编组器.

And this should be pretty enough to build unmarshaller or marshaller.

推荐答案

JAXB 的 JSON 支持怎么样?重用当前的 JAXB 注释模型类,但从 REST 端点输出 JSON.

How about JSON support for JAXB? Reuse your current JAXB annotated model classes but output JSON from your REST endpoints.

Jersey 的当前版本支持这一点(通过 jersey-json 模块)和 JSONJAXBContext.

Current versions of Jersey support this (via the jersey-json module) with JSONJAXBContext.

你也可以试试 JacksonJAXBJAX-RS 支持.

You could also try Jackson's JAXB and JAX-RS support.

这篇关于是否有用于 XML 绑定的 JavaScript API - 类似于 Java 的 JAXB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆