如何在 AngularJS 中处理 XML 服务? [英] How to handle XML services in AngularJS?

查看:33
本文介绍了如何在 AngularJS 中处理 XML 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司现有数以千计的 xml Web 服务,并且开始在新项目中采用 AngularJs.

My company has thousands of existing xml web services and is starting to adopt AngularJs for new projects.

http://angularjs.org/ 上的教程仅使用 json 服务.看起来他们在控制器中进行服务调用,解析生成的 JSON,并将生成的对象直接传递给视图.

The tutorial over at http://angularjs.org/ uses json services exclusively. It looks like they make a service call in the controller, parse the resulting JSON, and pass the resulting object directly to the view.

我如何处理 XML?我看到四个选项:

What do I do with XML? I see four options:

  1. 解析它并将 DOM 对象直接传递给 UI(视图).

  1. Parse it and pass the DOM object directly to the UI(view).

在服务器端为我的 XML 服务放置一个 JSON 包装器.

Put a JSON wrapper around my XML services on the server side.

使用客户端的某个库将 DOM 对象转换为 JSON,并在我发出 post/put 请求时将其转换回来.

convert the DOM object to JSON with some library on the client side and convert it back when I make the post/put requests.

在客户端手动将 DOM 对象转换为 JavaScript 对象.

Convert the DOM object to a JavaScript object manually on the client side.

正确的方法是什么?为什么?

What the correct approach and why?

推荐答案

如果选项 2 对您来说相对容易(例如在后端控制器中添加一行 JSON 转换),那么它可能是一项不错的投资,因为 JSON 在网络上更精简,客户端的工作要少得多,而且通常受到 RESTful API 消费者的青睐(以防有其他消费者).

If option 2 is relatively easy for you (like adding one-line JSON conversions in your back-end controllers, for example), then it's probably a good investment, as the JSON is leaner over the wire, far less work on the client side and generally preferred by RESTful API consumers (in case there are other consumers).

最近完成了这种工作,我想说下一个最佳路径(如果选项 2 很难)是使用响应和请求 transformers 来执行 XML 和 JavaScript 对象之间的转换,这是选项 3 和 4 之间的变体.DOMParser 对象是本机代码,因此它可以大量解析 XML快速地.将 XML DOM 转换为 JavaScript 对象和从 JavaScript 对象生成 XML 都是相当简单的递归算法.这种方法将所有其余的客户端代码与后端表示分离,如果您采用选项 1,则情况并非如此.这种分离将允许您直接使用基于 JSON 的 RESTful 接口,如果出现这样的机会.

Having recently done this kind of work, I'd say the next best path (if option 2 is difficult) would be to use response and request transformers to perform conversions between your XML and JavaScript objects, which is a variation somewhere between your options 3 and 4. The DOMParser object is native code, so it parses XML plenty fast. Transforming the XML DOM to JavaScript objects and generating XML from JavaScript objects are both fairly straightforward recursive algorithms. This approach decouples all of the rest of your client-side code from the back-end representation, which would not be the case if you went with your option 1. Such decoupling would allow you to make direct use of a JSON-based RESTful interface, should such an opportunity arise.

选择任何涉及 JSON/JavaScript 对象的选项通常会涉及处理阻抗不匹配问题,例如 XML 属性、XML 集合与 JS 数组以及 XML 混合内容表示.如果您的数据模型足够简单,或者您不介意使用 XML 和 JSON 之间的开箱即用转换器提供的解决方案(例如,冗余对象包含、编号文本属性以表示与元素混合的不相交文本),那么这对您来说可能不是问题.否则,有机会在请求的任一端强制自定义转换行为(尽管遗憾的是,据我所知,不是声明性的).

Selecting any option that involves JSON/JavaScript objects will often involve dealing with impedance mismatch issues like XML attributes, XML collections vs. JS arrays and XML mixed content representation. If your data models are simple enough, or you don't mind living with the solutions provided by out-of-the-box transformers between XML and JSON (e.g., redundant object containment, numbered text properties to represent disjoint text mixed with elements), then this may not be an issue for you. Otherwise, there are opportunities for customizing transformation behavior at either end of the request imperatively (though sadly not declaratively, as far as I've seen).

这篇关于如何在 AngularJS 中处理 XML 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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