Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert XML to JSON in AngularJS

Shulou Source: shulou.com Published: 2022-06-02 14:37:49 09月25日 Update

This article shows you how to convert XML to JSON in AngularJS. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

We'll talk about how to convert XML files to JSON in Angular JS. Everyone knows that Angular JS is the JavaScript framework for developing applications. So basically the responsive JSON format that Angular JS expects. Therefore, before you start working on the data, it is recommended that you return the data in JSON format. We will use the Angular JS $http service to load a local XML file and convert the XML file to JSON.

Background

I once published an article about the Angular JS $http service: "$http Service In Angular JS"

source code

Download source code: XML to JSON Source Code

Use the code

First create a HTML page.

Convert XML to JSON In Angular JS-SibeeshPassion

Now, add the required references as follows.

Have you noticed that I have added the xml2json.js file? This is the file to be converted. You can download files from https://code.google.com/p/x2js/.

Now create a controller and application instruction, as shown below.

The next thing we need to do is add services. You can add a $http service as follows.

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

Here httpApp is our application and httpController is our controller. We use the function transformResponse to transform the response.

Conversion request and response

In Angular JS, the request can be transformed using the transformRequest function, or if it is a response, through the function transformResponse. These functions return the converted values.

Here is an example of how we use the transformResponse function.

TransformResponse: function (cnv) {

Var x2js = new X2JS ()

Var aftCnv = x2js.xml_str2json (cnv)

Return aftCnv

}

This x2js.xml_str2json (CNV) will return the JSON object and aftCnv from the transformResponse function. Doesn't that sound good? Once we're ready, we just need to write the JSON object in the browser console and we can see it.

Output

Converting XML to JSON in Angular JS is as simple as that.

The above is how to convert XML to JSON in AngularJS. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Functions files services objects that is applications programs applications controls content skills controllers data articles formats source code knowledge good concise next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei NVidia Apple OPPO Reno