Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert XML to JSON in AngularJS

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report