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

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

问题描述

在Java中,我们使用JAXB2进行了大量工作。对象< - > 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运行时可以读取这些注释并创建unmarshaller以将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处理。例如,我们需要解析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 for JavaScript有哪些模拟?

一些工具可以将XML Schema编译成一些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()
      })
    }
  ]
});

这应该足以构建unmarshaller或marshaller。

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

推荐答案

JSON对JAXB的支持如何?重用当前带有JAXB注释的模型类,但从REST端点输出JSON。

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

当前版本的泽西岛支持这一点(通过 jersey = json 模块) JSONJAXBContext

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

您也可以尝试 Jackson JAXB JAX-RS 支持。

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

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

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