是否有用于 XML 绑定的 JavaScript API - 类似于 Java 的 JAXB?

发布于 2024-09-25 11:59:33 字数 1439 浏览 1 评论 0原文

在 Java 中,我们大量使用 JAXB2。对象<-> XML 映射被定义为 Java 类中的注释:

@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 附带了一个模式编译器 (XJC),它可以从 XML 模式生成带注释的类,这是另一个很棒的功能。


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

我的问题是:

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

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

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

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

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

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 runtime can read these annotations and create unmarshaller to parse XML into an object instance or marshall an object into XML.

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


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.

My question is:

Is there some analog of JAXB for 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.

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

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

发布评论

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

评论(4

诗化ㄋ丶相逢 2024-10-02 11:59:33

到目前为止,我还没有找到与我需要的类似的东西。因此我决定自己实现它。这是项目页面:

http://confluence.highsource.org/display/MISC/Jsonix

该项目托管在 GitHub 上:

https://github.com/highsource/jsonix/

To date, I have found nothing similar to what I need. Therefore I've deсided to implement it myself. Here's the project page:

http://confluence.highsource.org/display/MISC/Jsonix

The project is hosted on GitHub:

https://github.com/highsource/jsonix/

波浪屿的海角声 2024-10-02 11:59:33

我还没有尝试过这个,所以我不确定它是否会起作用,但是您是否考虑过使用 GWT,以便您仍然可以使用 JAXB 并将整个应用程序编写为 java 应用程序?我不确定 GWT 是否支持 JAXB(可能不支持),但可能有它支持的 xml 解析的替代方案。如果这有效,您可以通过 gwt 自动创建 javascript 模型,然后将它们包含到您的应用程序中。是的,它比您想要的要粗糙得多,但胜过从头开始编写它。

I haven't tried this, so I'm not sure if it will work, but have you considered using GWT so that you can still use JAXB and write the whole app as a java app? I'm not sure if GWT supports JAXB (probably not), but there might be an alternative for xml parsing that it will support. If this works, you could automate the creation of your javascript models via gwt, and then include these into your app. Yes, it's a lot more cruft than you want, but beats having to write it from scratch.

恍梦境° 2024-10-02 11:59:33

您可以做的就是向 XML 添加通用样式表定义:XSLT,以将它们转换为 JSON。
例如:http://code.google.com/p/xml2json-xslt/

与 JSON 相比,使用 Javascript 处理 XML 是一件痛苦的事情,尤其是跨浏览器。
样式表会给您的请求增加一点开销。无论是在服务器端还是在客户端,您都可以选择,但是您必须将其与代码复杂性以及在不同浏览器上使用 Javascript 解析和读取 XML 的速度进行比较。

What you can do is add a generic stylesheet definition: XSLT to your XML to convert them in JSON.
eg: http://code.google.com/p/xml2json-xslt/

Handling XML with Javascript is a pain compared to JSON, especially cross browser.
The stylesheet will add a small overhead to your request. Either on the server or client side, you can choose, but you have to compare this to the code complexity and speed to parse and read the XML with Javascript on different browsers.

泪是无色的血 2024-10-02 11:59:33

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

当前版本的 Jersey 支持此功能(通过 jersey-json 模块)与 JSONJAXBContext

您还可以尝试 JacksonJAXBJAX-RS 支持。

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

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

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

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