Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle XML services in AngularJS?

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

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.

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

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

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

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

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

What the correct approach and why?

like image 703
Nick Vikeras Avatar asked Mar 19 '13 02:03

Nick Vikeras


People also ask

Can we use XML in angular?

With Angular, the CData API Server, and the ADO.NET Provider for XML (or any of 200+ other ADO.NET Providers), you can build single-page applications (SPAs) with access to live data from XML.

What is$ http in AngularJS?

$http is an AngularJS service for reading data from remote servers.

How do I display XML in angular 8?

After creating the project, open the project in your favorite editor and install the "timers" npm package. This package is necessary for reading an XML file with xml2js package. Open the index. html present at root folder and add a reference for Bootstrap and jQuery.

What is AngularJS used for?

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.


1 Answers

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).

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.

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).

like image 95
Jollymorphic Avatar answered Sep 25 '22 12:09

Jollymorphic