Sibeesh Passion

Convert XML to JSON In Angular JS


In this post we will see how we can convert an XML file to JSON in Angular JS. As you all know Angular JS is a JavaScript framework for developing applications. So basically Angular JS 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 Angular JS $http service, and we will convert the same XML file to JSON. If you are new to Angular JS, please read here: Angular JS. I hope you will like this article.


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

Source Code

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

Using the code

Create an html page first.

<!DOCTYPE html>
    <title>Convert XML to JSON In Angular JS - SibeeshPassion </title>

Now add the needed reference as follows.

<script src="jquery-2.1.3.min.js"></script>
    <script src="angular.min.js"></script>
    <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

Now create a controller and app directive as follows.

  <div ng-app="httpApp" ng-controller="httpController">

Next thing we need to do is adding the service. You can add the $http service as follows.

        var app = angular.module('httpApp', []);
        app.controller('httpController', function ($scope, $http) {
                        transformResponse: function (cnv) {
                            var x2js = new X2JS();
                            var aftCnv = x2js.xml_str2json(cnv);
                            return aftCnv;
            .success(function (response) {

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 Angular JS, 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.

transformResponse: function (cnv) {
                            var x2js = new X2JS();
                            var aftCnv = x2js.xml_str2json(cnv);
                            return aftCnv;

The x2js.xml_str2json(cnv) will return the json object and we return aftCnv from the function transformResponse. Sounds good? Once we are done everything, we are just write the JSON object in the browser console, so that we can see the object.


Convert XML To JSON In Angular JS

Convert XML To JSON In Angular JS

That’s all we have done everything. Happy coding!.


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?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Kindest Regards
Sibeesh Venu

  • Vasu Eedpuganti

    Nice post…

  • Ben Silburn

    Great module! I tried it out in an Angular site and it worked straight out of the box. Perfect!

    • I am glad it worked for you. Thanks much for your feedback. I really appreciate it.

  • César Castro Aroche

    Thank u so much!!!!!

    • You are welcome, thanks much for your feedback. I really appreciate it.

  • Vinod Joshi

    how we can xml tag print in html find the tag value ?

    var word=$(this).text();
    arrwords[j] = word;
    //if( j < 5 )
    // arrword.word_items.push(word);

    and angular ?

Footer With Address And Phones