Convert XML To JSON In AngularJS

As you all know AngularJS is a JavaScript framework for developing applications. So basically AngularJS expects the response in the form of JSON. Hence it is recommended to return the data in JSON format before you start to work on the data. Here in this post we will load a local XML file using AngularJS $http service, and we will convert the same XML file to JSON. If you are new to AngularJS, please read here: Angular JS. I hope you will like this article.

Background

I have already posted an article related to $http service in Angular JS, you can see here.

Source Code

Please download the source code here: XML to JSON Source Code

Using the code

Create an html page first,
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Convert XML to JSON In Angular JS - SibeeshPassion </title>  
  6. </head>  
  7.   <body>   
  8.   </body>  
  9.   
  10. </html>  
Now add the needed reference as follows:
  1. <script src="jquery-2.1.3.min.js"></script>  
  2. <script src="angular.min.js"></script>  
  3. <script src="xml2json.js"></script>  
Have you noticed that I have added xml2json.js file? This is the file which is doing the conversion part. You can always download the file from the following link.

Now create a controller and app directive as follows::
  1. <div ng-app="httpApp" ng-controller="httpController">  
  2.   </div>  
Next thing we need to do is adding the service. You can add the $http service as follows:
  1. <script>  
  2. var app = angular.module('httpApp', []);  
  3. app.controller('httpController', function($scope, $http)  
  4. {  
  5.     $http.get("Sitemap.xml",  
  6.     {  
  7.         transformResponse: function(cnv)  
  8.         {  
  9.             var x2js = new X2JS();  
  10.             var aftCnv = x2js.xml_str2json(cnv);  
  11.             return aftCnv;  
  12.         }  
  13.     }).success(function(response)  
  14.     {  
  15.         console.log(response);  
  16.     });  
  17. });  
  18. </script>  
Here httpApp is our app and httpController is our controller. And we are transforming our response using the function transformResponse.

Transforming Request and Response

In AngularJS, requests can be transformed using a function called transformRequest and if it is a response we can transform by function transformResponse. These functions returns the transformed values.

Here in our example we are using the transformResponse function as follows:
  1. transformResponse: function(cnv)  
  2. {  
  3.     var x2js = new X2JS();  
  4.     var aftCnv = x2js.xml_str2json(cnv);  
  5.     return aftCnv;  
  6. }  
The x2js.xml_str2json(cnv) will return the json object and we return aftCnv from the function transformResponse. Sounds good? Once we are done with everything, we are just writing the JSON object in the browser console, so that we can see the object.

Output

Convert XML To JSON In Angular JS

That’s all we have done everything.

Conclusion

Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

If you have any questions, then please mention in the comments section.


Similar Articles